大佬教程收集整理的这篇文章主要介绍了Node.js,Ajax发送和接收Json,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在服务器端尝试此操作:
var port = 8000;
var http = require("http");
var server = http.createServer();
server.on('request', request);
server.Listen(port);
function request(request, responsE) {
var store = '';
request.on('data', function(data)
{
store += data;
});
request.on('end', function()
{ console.log(storE);
response.setheader("Content-Type", "text/Json");
response.setheader("Access-Control-Allow-Origin", "*");
response.end(storE)
});
}
在客户端:
$.AJAX
({
type: "POST",
url: "http://localhost:8000",
crossDomain:true,
dataType: "Json",
data:JsON.Stringify({name: "Dennis", address: {City: "dub", country: "IE"}})
}).done(function ( data ) {
alert("AJAX callBACk response:"+JsON.Stringify(data));
})
希望这对你有用
使用Ajax,我试图仅将Json数据发送到节点服务器,不进行任何处理,仅在发送时发出警报,在接收到时发出警报:
这是我的html5:具有onclick函数的简单按钮,可触发该函数使用ajax调用
<!DOCTYPE HTML>
<html>
<head>
<script>
function send()
{
//alert("Hello World");
$.ajax
({
type: "post",url: "http://localhost:8000",dataType: "json",contentType: "application/json; charset=UTF-8",data: JSON.Stringify({name: "Dennis",address: {City: "Dub",country: "IE"}})
}).done(function ( data ) {alert("ajax callBACk response:" + data);
});
</script>
</head>
<body>
<button onclick="send()">Click Me!</button>
</body>
</html>
这是节点服务器的一部分:用于创建服务器并侦听某些操作
var port = 8000;
var server = http.createServer();
server.on('request',request);
server.listen(port);
function request(request,responsE)
{
var store = '';
response.writeHead(200,{"Content-Type": "text/json"});
request.on('data',function(data)
{
store += data;
});
request.on('end',function()
{
store = JSON.parse(storE);
console.log(storE);
response.end(storE);
});
}
没有警报被触发,所以我不认为ajax正在尝试发送信息。
以上是大佬教程为你收集整理的Node.js,Ajax发送和接收Json全部内容,希望文章能够帮你解决Node.js,Ajax发送和接收Json所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。