0
我一直盯着这个6个小时。我不明白。为什么我的jquery.ajax()不会触发成功或错误?
$.ajax({
url: "http://www.band.dev:8888/datafeeder/hello_world",
success: function(data) {
alert (data);
},
error: function(request, status, error) {
alert(status + ' - ' + error);
}
});
我跑甲基苯丙胺在本地,当我直接打网址它的回声“世界你好”,没有任何问题。当我运行这个,我得到一个对话框'错误 - '。我试着在里面添加dataType:'html',没有任何帮助。思考?
- 编辑 -
所以这是我的实际问题。当我运行这个时,没有成功或错误被激发,并且当我直接点击URL时,我可以看到JSON是正确的。 (顺便说一句,相对URL修复工作了上面的代码位)。
$("#member_type").autocomplete({
source: function(request, response) {
$.ajax({
url: "/datafeeder/get_member_types/json",
dataType: "jsonp",
data: {
//maxNum: 12,
searchString: request.term
},
search: function(event, ui) {
alert('searching');
},
success: function(data) {
alert (data);
response($.map(data, function(item) {
return {
label: item.type,
value: item.id
}
}));
},
error: function(request, status, error) {
alert (status + ' - ' + error);
}
});
}
});
我之所以这么想是因为我的原始背景是在Flash开发中,而跨域策略是非常必要和常见的。但有了这个,我看到http://jqueryui.com/demos/autocomplete/#remote-jsonp用于自动完成输入,并且他们正在从外部托管的脚本中提取。这仅仅是因为他们返回的JSONP格式? – Tim
@Tim,对于JSONP来说,你的服务器必须返回JSONP,而不是''hello world''。一个JSONP看起来可能看起来像这个'someCallback({value:'hello world'})'当然你可以通过传递请求参数来个性化'someCallback'的名字。 –
http://stackoverflow.com/questions/2887209/what-are-the-differences-between-json-and-jsonp/2887218#2887218这可能有助于理解差异 – Rafay