我正在使用以下代码进行跨域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;
}
}
});
Run Code Online (Sandbox Code Playgroud)
在Firebug中调试时,我收到以下错误:

SyntaxError: missing ; before statement
Run Code Online (Sandbox Code Playgroud)
但是,当我通过像jsonlint.com这样的工具传递我的json对象(通过JQ代码中的链接可用)时,它说它是有效的JSON.我也没有发现任何异常现象.怎么会返回语法错误?它是一些JSONP细节我没有得到或什么?
{"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", …Run Code Online (Sandbox Code Playgroud) 我搜索了FOREVER,无法对我的问题做出明确的答案.所以这就是.我有一个JSON文件(我去jsonlint验证,它说它很好)看起来像这样(一些信息修改):
[{
"position":"1",
"category":"A",
"title":"Title to first story",
"description":"The first story."
},
{
"position":"2",
"category":"B",
"title":"Title to second story",
"description":"The second story"
},
{
"position":"3",
"category":"B",
"title":"Title to third story",
"description":"The third story"
}
]
Run Code Online (Sandbox Code Playgroud)
我使用jQuery解析并使用此函数放置一个html页面:
$.getJSON('page.json', function(data) {
var items = [];
$.each(data.reponse, function(item, i) {
items.push('<li id="' + i.position + '">' + i.title + ' - ' + i.description + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
Run Code Online (Sandbox Code Playgroud)
它完美无缺!现在我的问题是,JSON文件不会在本地托管,实际上将托管在一个单独的域上.所以我修改了我的代码如下(经过一些阅读)希望让它工作:
$.getJSON('http://www.otherdomain.com/page.json?format=json&callback=?', function(data) {
var items = []; …Run Code Online (Sandbox Code Playgroud)