Nodejs Express,Ajax 发布/jquery 并接收响应
在让 Express 正确响应我的 jquery ajax 请求时遇到一些问题。实际的发布工作正常,但无论我尝试什么,我似乎都无法从我可以使用的应用程序中实际获得数据响应。起初,它只是不断地发布和挂起,大约一分钟后,它会响应一个警报,上面写着“XML 文档已加载”(不知道它来自哪里)——无论如何,现在它给了我
SyntaxError:意外的令牌非法 解析时(本地) 在传入消息。
在我的 Express 应用程序中,我有:
app.post('/save', function(req, res) {
console.log(req.body.objectData);
res.contentType('json');
res.send({ some: 'json' });
});
在我的 jquery 中:
$.ajax({
url: "/save",
type: "POST",
dataType: "json",
data: {objectData: someObject},
contentType: "application/json",
cache: false,
timeout: 5000,
complete: function() {
//called when complete
console.log('process complete');
},
success: function(data) {
console.log(data);
console.log('process sucess');
},
error: function() {
console.log('process error');
},
});
Having some trouble getting express to respond properly to my jquery ajax request. The actual posting is working fine, but no matter what I try I cant seem to actually get a data response from my app that I can use. At first it was just posting and hanging constantly, and like a minute later it would respond with an alert that said something like "XML Document loaded" (have no idea where it was coming from) -- Anyways, now its giving me
SyntaxError: Unexpected token ILLEGAL
at parse (native)
at IncomingMessage.
In my express App, I have:
app.post('/save', function(req, res) {
console.log(req.body.objectData);
res.contentType('json');
res.send({ some: 'json' });
});
and in my jquery:
$.ajax({
url: "/save",
type: "POST",
dataType: "json",
data: {objectData: someObject},
contentType: "application/json",
cache: false,
timeout: 5000,
complete: function() {
//called when complete
console.log('process complete');
},
success: function(data) {
console.log(data);
console.log('process sucess');
},
error: function() {
console.log('process error');
},
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有发送有效的 JSON 响应,而是发送包含单词
json
的字符串,因此JSON.parse()
在客户端失败。试试这个:JavaScript 对象表示法 是一种在应用程序之间以对象格式共享数据的方法。但是,如果不首先将对象转换为字符串并将其作为单个变量发送,则无法通过 HTTP 请求发送对象。函数
JSON.parse()
和JSON.stringify()
为我们完成了这个任务。You are not sending a valid JSON response but a String containing the word
json
therefore theJSON.parse()
is failing on the client side. Try this:JavaScript Object Notation is a way to share data between applications in object format. However, you cannot send an object over an HTTP request without first turning it into a string and sending it as a single variable. The functions
JSON.parse()
andJSON.stringify()
do this for us.博恩斯牧师的评论对我来说特别重要,因为我正在使用 $.ajax 发布到 Node 服务器。我的相关代码部分最终如下所示:
请注意,contentType 标头与解析工作相关。
在节点服务器端,我可以像这样处理有效负载:
Pastor Bones' comment was particularly important to me, as I was using $.ajax to post to a Node server. My relevant portion of code ended up like this:
Note that the contentType header is relevant for the parsing to work.
On the node server side, I can process the payload like this:
由于您使用的是express,
应该是:
但不需要设置类型,因为它是自动为您完成的。
请参阅有关 res.type 的express api 文档。
另请注意,对于express,
res.send({blah:"gee"});
会在内部使用JSON.stringify自动转换json对象。单击上述链接后,单击res.send
,然后单击res.json
,当您知道正在发送 JSON 时,这会节省一点处理器开销。请注意,如果您发送 JSON,类型会自动设置为 JSON。最好始终查看源代码!请注意,res.send 在检测到 JSON 时调用 this.json,并且 res.json 调用 this.send(是的,看起来像一个循环,但一切正常)。
Since you are using express,
should be:
but setting the type is not required since it is done automatically for you.
See the express api docs on res.type.
Also note that, for express,
res.send({blah:"gee"});
automatically converts json objects using JSON.stringify internally. After clicking the above link, click onres.send
and, while you are at it,res.json
which saves a little processor overhead when you know you are sending JSON. Note that if you send JSON, the type is automatically set to JSON.Always best to look at the source! Note that res.send calls this.json when it detects JSON, and that res.json calls this.send (yeah seems like a loop but it all works out).