javascript jQuery.ajax#get 之后出现意外的令牌冒号 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24311023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unexpected token colon JSON after jQuery.ajax#get
提问by K. L.
I have created a minimalist API on nodejswhich return data in JSON format.
我在nodejs上创建了一个极简的 API,它以 JSON 格式返回数据。
But every time I try to make a ajax#get call and pass my API as URL, I will get an error and judging from Chrome, I'm getting a "Unexpected token :"
error;
但是每次我尝试进行 ajax#get 调用并将我的 API 作为 URL 传递时,我都会收到一个错误,从 Chrome 来看,我收到了一个"Unexpected token :"
错误;
here the server code in nodejs+ express:
这里是nodejs+ express 中的服务器代码:
var
http = require( 'http' ),
express = require( 'express' ),
app = express(),
server = http.createServer( app );
app.get( '/', function( req, res ) {
console.log( 'req received' );
res.setHeader('Content-Type', 'application/json');
res.end( JSON.stringify({
Name : "Tom",
Description : "Hello it's me!"
}) );
});
server.listen(3000, function() {
console.log( 'Listening on 3000' );
});
The JSON returned from "/"
is: {"Name":"Tom","Description":"Hello it's me!"}
.
返回的 JSON"/"
是:{"Name":"Tom","Description":"Hello it's me!"}
。
Here is my call from the client js:
这是我从客户端 js 打来的电话:
$.ajax({
url: findUrl,
type: 'get',
dataType: 'jsonp',
success: function ( data ) {
self.name( data.Name );
self.description( data.Description );
},
error: function( jqXHR, textStatus, errorThrown ) {
alert(errorThrown);
}
});
When plotting the error I get: "jQuery111108398571682628244_1403193212453 was not called"
绘制错误时,我得到: "jQuery111108398571682628244_1403193212453 was not called"
Can someone help me?
有人能帮我吗?
I know this question has been asked already but I haven't manage to find a solution which fix my program.
我知道已经有人问过这个问题,但我还没有找到修复我的程序的解决方案。
回答by Jonathan Lonowski
To support JSONP requests, the server will have to include the P
, or "Padding," in the response.
为了支持JSONP 请求,服务器必须在响应中包含P
或“ Padding”。
jQuery111108398571682628244_1403193212453({"Name":"Tom","Description":"Hello it's me!"})
The syntax error, "Unexpected token :"
, is because JSONP is parsed as JavaScript, where {...}
also represents blocks. It just takes advantage of JSON and JavaScript's similar syntax to define the data being passed to a global function call.
语法错误,"Unexpected token :"
是因为 JSONP 被解析为 JavaScript,其中{...}
也表示blocks。它只是利用 JSON 和 JavaScript 的类似语法来定义传递给全局函数调用的数据。
By default, jQuery will include a callback
query-string parameter with the name of the function:
默认情况下,jQuery 将包含一个callback
带有函数名称的查询字符串参数:
var callback = req.query.callback;
var data = JSON.stringify({
Name : "Tom",
Description : "Hello it's me!"
});
if (callback) {
res.setHeader('Content-Type', 'text/javascript');
res.end(callback + '(' + data + ')');
} else {
res.setHeader('Content-Type', 'application/json');
res.end(data);
}
ExpressJS also includes res.jsonp()
that already implements this condition:
ExpressJS 还包括res.jsonp()
已经实现了这个条件的:
app.get( '/', function( req, res ) {
console.log( 'req received' );
res.jsonp({
Name : "Tom",
Description : "Hello it's me!"
});
});
回答by Dan Putman
You want to use dataType: "json" instead of "jsonp"
您想使用 dataType: "json" 而不是 "jsonp"