【发布时间】:2013-07-30 18:06:05
【问题描述】:
我正在尝试 .ajax 访问返回对象列表的 URL,我在 spring 中使用 jacksonJsonView 以在浏览器中返回 json。但是当我尝试这段代码时,它永远不会成功,但错误警报显示 textstatus 为“parseerror”。警报如下所示:'status=parsererror,error=jQuery15109695890768120119_1357924928198 was not called'
$(function() {
$("#tags").autocomplete({
source: function( request, response ) {
$.ajax({
url: 'http://localhost:8181/jquery/api/states/regex?stateName='+request.term,
method: 'GET',
dataType: 'jsonp',
success: function(json) {
alert("test");
},
error: function(httpRequest, textStatus, errorThrown) {
alert("status=" + textStatus + ",error=" + errorThrown);
}
});
}
})
API 返回如下内容:
[
{
"id": 12,
"stateName": "Vermont",
"intPtLon": -72.673354,
"intPtLat": 44.0605475,
"stUsps": "VT"
},
{
"id": 20,
"stateName": "Virginia",
"intPtLon": -78.6681938,
"intPtLat": 37.5222512,
"stUsps": "VA"
}
]
添加以下行解决了这个问题,它在 IE 中运行良好,而在 firefox 中则没有那么多
【问题讨论】:
-
您确定需要 JSONP 吗?
-
如果我删除它,它会给出“status=error,error="
-
看我的回答。假设将
dataType: 'jsonp',更改为dataType: 'json',就足够了。