正在使用 RESTful api,当我在浏览器中通过 96.126.111.211/getLeaderBoard 访问此 API 时
它工作正常,并为我提供了 mongo 数据库中的 JSON 数据。
尽管当我尝试像这样的 jquery ajax 请求时 http://jsfiddle.net/yVQ5T/我收到 jsonp 错误
Error: jQuery171037622810900211334_1363808084624 was not called
这是服务器端的代码
getLeaderBoard(req, res)
{
this.db.collection(this.settings.userTable, function(err, collection) => {
collection.find().toArray(function(err, items) {
res.writeHead(200, {'Content-Type': 'application/javascript'});
res.write(JSON.stringify(items));
res.end();
});
});
}
最佳答案
您的网址提供常规 JSON,而不是 JSONP,您必须将其放在对回调函数的调用中,例如
jQuery171037622810900211334_1363808084624 ([{"userId":"100821494565115211032","winCount":4,"_id":"5148caa1a1651dff12000002"}]);
由于回调名称是动态生成的,因此您必须从请求中读取它们。
关于jquery - Node.js Express jquery ajax 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15533132/