Node.js,Ajax发送和接收Json [英] Node.js, Ajax sending and receiving Json
本文介绍了Node.js,Ajax发送和接收Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的html5:简单按钮,带有onclick函数以触发函数使用ajax调用。
<!DOCTYPE HTML>
< html>
< head>
< script>
函数send()
{
// alert(Hello World);
$ .ajax
({
类型: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);
函数请求(请求,响应)
{
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。结束(商店);
});
}
没有警报被解雇,所以我不认为ajax正在尝试发送信息。
解决方案
var port = 8000;
var http = require(http);
var server = http.createServer();
server.on('request',request);
server.listen(port);
函数请求(请求,响应){
var store ='';
$ b 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,地址:{city:Dub,国家:IE}} })
})。done(function(data){
alert(ajax callback response:+ JSON.stringify(data));
})
希望这对您有用
Using Ajax, I'm trying to just send Json data to a node server no processing involved just alerting when it's sent and alerting when it's received:
This is my html5: Simple button with an onclick function to trigger the function to use the ajax call
<!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>
This is a portion of my node server: For creating a server and listening for certain actions
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);
});
}
No alerts are being fired so I don't think the ajax is attempting to send the information.
解决方案
try this on the server side:
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)
});
}
and this on the client side:
$.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));
})
Hope this works for you
这篇关于Node.js,Ajax发送和接收Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文