JQuery 中的 JSON / JSONP
我试图找出为什么我的 $.getJSON 方法似乎不起作用,但 $.ajax 工作得很好。首先,这是我的 getJSON 调用:
$.getJSON("http://localhost:1505/getServiceImageList?callback=loadImagesInSelect", loadImagesInSelect);
您可以看到我尝试将回调参数直接添加到查询字符串(也尝试不在字符串上添加),并且添加了对 js 文件中定义的回调方法的引用。
下面是运行良好的 $.ajax 调用:
function getImages() {
$.ajax({
type: "GET",
url: $('#txt_registry_url').val(),
dataType: "jsonp",
success:loadImagesInSelect ,
error:function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
在此示例中,从文本框中提取的 url 与直接调用 getJSON 中的 URL 相同。当方法调用完成时,会调用 successMethod,一切都会正常进行。
虽然我很喜欢使用这两种方法中的后者,但文档使 getJSON 似乎是首选的简写方式。
谁能解释一下我在速记方法中缺少什么以使其全部正常工作?
提前致谢。
I am trying to figure out why my $.getJSON method does not seem to be working but the $.ajax works just fine. First, here is my getJSON call:
$.getJSON("http://localhost:1505/getServiceImageList?callback=loadImagesInSelect", loadImagesInSelect);
You can see I have tried added the callback parameter directly to the query string (also tried it not on string) and I added a reference to the callback method defined in my js file.
Here is the $.ajax call which works just fine:
function getImages() {
$.ajax({
type: "GET",
url: $('#txt_registry_url').val(),
dataType: "jsonp",
success:loadImagesInSelect ,
error:function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
In this example the url pulled from the text box is the same as in the straight call to getJSON. When the method call completes, the successMethod is called and everything processes just fine.
While I am cool with using the later of the two methods, the docs make it seem that the getJSON is the preferred shorthand way of doing things.
Can anyone please explain what I am missing on the shorthand method to make it all work?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然后使用 php 的服务器端(注意:我将数据添加到 GET 查询字符串)
getJSON 需要看到“callback=?”
我会坚持使用 $.ajax
then server side with php (note: I added data to the GET query string)
getJSON needs to see "callback=?"
I would stick with $.ajax
据我了解,您需要使用
jQuery 来为回调命名,然后将调用转发给 loadImagesInSelect
我希望这会有所帮助
杰罗姆·瓦格纳
from what I understand you need to use
jQuery will take care of giving a name to the callback and then forwarding the call to loadImagesInSelect
I hope this will help
Jerome WAGNER