I am making a cross domain JSONP call using this code:
我正在使用以下代码进行跨域JSONP调用:
jQuery.ajax({
async: true,
url: 'http://mnews.hostoi.com/test.json',
dataType: 'jsonp',
method: "GET",
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus + ': ' + errorThrown);
},
success: function (data, textStatus, jqXHR) {
if (data.Error || data.Response) {
exists = 0;
}
}
});
When debugging in Firebug, I get the following error:
在Firebug中调试时,我得到如下错误:
SyntaxError: missing ; before statement
However, when I pass my json object (available through the link in the JQ code) through a tool like jsonlint.com, it says it is valid JSON. And I don't find any anomalies either. How could it be returning a syntax error? Is it some JSONP detail I am not getting or what?
但是,当我通过jsonlint.com之类的工具传递json对象(可以通过JQ代码中的链接获得)时,它说它是有效的json。我也没有发现任何异常。它怎么会返回语法错误呢?是我没有得到一些JSONP细节还是什么?
JSON Sample
{"news":[ {
"sentences": [
"Neuroscientists have discovered abnormal neural activity...",
"The researchers found that these mice showed many symptoms...",
"\"Therefore,\" the study authors say, \"our findings provide a novel.."
],
"summaryId": "ZJEmY5",
"title": "Abnormal neural activity linked to schizophrenia"
}]}
Thanks in advance.
提前谢谢。
6 个解决方案
#1
29
JSONP is not JSON. A JSONP response would consist of a JavaScript script containing only a function call (to a pre-defined function) with one argument (which is a JavaScript object literal conforming to JSON syntax).
JSONP是JSON。JSONP响应将由一个JavaScript脚本组成,该脚本只包含一个函数调用(对预定义函数)和一个参数(一个符合JSON语法的JavaScript对象文本)。
The response you are getting is JSON, not JSONP so your efforts to handle it as JSONP fail.
您得到的响应是JSON,而不是JSONP,因此您在JSONP失败时处理它的努力。
Change dataType: 'jsonp'
to dataType: 'json'
(or remove the line entirely, the server issues the correct content-type so you don't need to override it).
将dataType: 'jsonp'更改为dataType: 'json'(或完全删除该行,服务器发出正确的内容类型,因此不需要覆盖它)。
Since your script is running on a different origin to the JSON then you will also need to take steps (most, but not all, of which require that you control the host serving the JSON) to work around the same origin policy.
由于您的脚本运行在与JSON不同的起点上,所以您还需要采取步骤(大多数步骤,但不是全部步骤,这些步骤要求您控制服务JSON的主机)来处理相同的起源策略。
#2
7
The error is because it is returning JSON not JSONP.
错误在于它返回的是JSON而不是JSONP。
JSONP is supposed to look like
JSONP应该是这样的
someCallBackString({ The Object });
#3
4
Here is the working example
下面是一个工作示例
$.ajax({
type: 'GET',
url: 'http://xxx.amazonaws.com/file.json',
dataType: 'jsonp',
jsonpCallback: 'callback',
success: function(json){
console.log(json);
}
});
And you should put callback
in the beginning of your file.json
like :
你应该在文件的开头写上callback。json:
callback({"item":{".......
回调({“项”:{ " .......
#4
2
As epascarello pointed out, the JSONP response must be sent like:
正如epascarello所指出的,JSONP的响应必须如下发送:
callBackFunction({ JSON Object })
And the caller function can then be setup like:
调用方函数可以设置为:
var url = "http://someremoteurl.com/json";
$.getJSON(url + "?callback=?", null, function(data) {
callBackFunction(data);
});
Then you can loop over the response data as:
然后将响应数据循环如下:
function callBackFunction(data)
{
console.log(data);
}
#5
1
If you're using "callback=?" parameter, your response on the server side should look like this:
如果您使用的是“callback=?”参数,那么服务器端的响应应该是这样的:
$_callback = $_GET['callback'];
echo $_callback . '(' . json_encode(YOUR_VARIABLE) . ');';
If "callback=?" parameter is not defined, your response should look like this:
如果“callback=?”参数没有定义,您的响应应该是这样的:
echo '[' . json_encode($_return_array) . ']';
#6
0
If the question is related to Ruby then in your controller make sure that you render the format correctly. example:
如果问题与Ruby有关,那么请在控制器中确保正确呈现格式。例子:
def view_product
data = Product.find params[:id]
render :json => data, :callback => params[:callback]
end
In your render method, you should have the :callback parameter otherwise it will render in json instead of jsonp.
在呈现方法中,应该有:callback参数,否则它将以json而不是jsonp呈现。
#1
29
JSONP is not JSON. A JSONP response would consist of a JavaScript script containing only a function call (to a pre-defined function) with one argument (which is a JavaScript object literal conforming to JSON syntax).
JSONP是JSON。JSONP响应将由一个JavaScript脚本组成,该脚本只包含一个函数调用(对预定义函数)和一个参数(一个符合JSON语法的JavaScript对象文本)。
The response you are getting is JSON, not JSONP so your efforts to handle it as JSONP fail.
您得到的响应是JSON,而不是JSONP,因此您在JSONP失败时处理它的努力。
Change dataType: 'jsonp'
to dataType: 'json'
(or remove the line entirely, the server issues the correct content-type so you don't need to override it).
将dataType: 'jsonp'更改为dataType: 'json'(或完全删除该行,服务器发出正确的内容类型,因此不需要覆盖它)。
Since your script is running on a different origin to the JSON then you will also need to take steps (most, but not all, of which require that you control the host serving the JSON) to work around the same origin policy.
由于您的脚本运行在与JSON不同的起点上,所以您还需要采取步骤(大多数步骤,但不是全部步骤,这些步骤要求您控制服务JSON的主机)来处理相同的起源策略。
#2
7
The error is because it is returning JSON not JSONP.
错误在于它返回的是JSON而不是JSONP。
JSONP is supposed to look like
JSONP应该是这样的
someCallBackString({ The Object });
#3
4
Here is the working example
下面是一个工作示例
$.ajax({
type: 'GET',
url: 'http://xxx.amazonaws.com/file.json',
dataType: 'jsonp',
jsonpCallback: 'callback',
success: function(json){
console.log(json);
}
});
And you should put callback
in the beginning of your file.json
like :
你应该在文件的开头写上callback。json:
callback({"item":{".......
回调({“项”:{ " .......
#4
2
As epascarello pointed out, the JSONP response must be sent like:
正如epascarello所指出的,JSONP的响应必须如下发送:
callBackFunction({ JSON Object })
And the caller function can then be setup like:
调用方函数可以设置为:
var url = "http://someremoteurl.com/json";
$.getJSON(url + "?callback=?", null, function(data) {
callBackFunction(data);
});
Then you can loop over the response data as:
然后将响应数据循环如下:
function callBackFunction(data)
{
console.log(data);
}
#5
1
If you're using "callback=?" parameter, your response on the server side should look like this:
如果您使用的是“callback=?”参数,那么服务器端的响应应该是这样的:
$_callback = $_GET['callback'];
echo $_callback . '(' . json_encode(YOUR_VARIABLE) . ');';
If "callback=?" parameter is not defined, your response should look like this:
如果“callback=?”参数没有定义,您的响应应该是这样的:
echo '[' . json_encode($_return_array) . ']';
#6
0
If the question is related to Ruby then in your controller make sure that you render the format correctly. example:
如果问题与Ruby有关,那么请在控制器中确保正确呈现格式。例子:
def view_product
data = Product.find params[:id]
render :json => data, :callback => params[:callback]
end
In your render method, you should have the :callback parameter otherwise it will render in json instead of jsonp.
在呈现方法中,应该有:callback参数,否则它将以json而不是jsonp呈现。