【发布时间】:2015-12-25 05:14:09
【问题描述】:
我正在尝试解析 Json 文件响应,但遇到了麻烦。
我的 javascript 是:
var url=encodeURIComponent("http://localhost:8080/repositories/rep_name?query=some_query&Accept=application/sparql-results+json");
$.ajax({
url: url,
type:'GET',
dataType: 'jsonp',
success: function(data, textStatus, xhr) {
// do something
$('#doQuery').html("QUERY");
},
error: function(xhr, textStatus, errorThrown) {
_UTILS.showModal("error", textStatus, 300, 100);
}
});
但我在 chrome 中收到此错误“Uncaught SyntaxError: Unexpected token :”。我认为问题在于我的服务器给我的响应没有用 jsonp 回调包装,因为当我检查我的响应时,它显示为这样的普通 json 格式:
{
"head" : {
"vars" : [ "child", "parent", "count" ]
}
}
而不是这个:
callback({
"head" : {
"vars" : [ "child", "parent", "count" ]
}
})
但是当我检查来自 ajax 的请求 url 时,它实际上在 url 的末尾附加了“&callback=”参数,如下所示:
http://localhost:8080/repositories/rep_name?query=some_query&Accept=application/sparql-results+json&callback=jQuery21105273823537863791_1443406558624&_=1443406558625
那么为什么它仍然给我这个错误?
需要注意的一点是服务器发回了一个.srj文件,但是是json格式的,不知道是不是和报错有关系。另外,我不能使用 `dataType: 'json',否则我会得到“无法加载 XMLHttpRequest。请求的资源上没有 'Access-Control-Allow-Origin' 标头。”错误。我不认为我可以更改服务器。
【问题讨论】:
标签: jquery json ajax cross-domain jsonp