JQuery 中的 JSON / JSONP

发布于 2024-08-28 01:13:11 字数 907 浏览 2 评论 0原文

我试图找出为什么我的 $.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2024-09-04 01:13:11
$.getJSON("http://localhost:1505/getServiceImageList?data=yes&callback=?", loadImagesInSelect);

function loadImagesInSelect(json) {
   //whatever you want on success
}

然后使用 php 的服务器端(注意:我将数据添加到 GET 查询字符串)

$data = getDataAsJSON($_GET['data']);
echo $_GET['callback'] . '(' . $data . ');';

getJSON 需要看到“callback=?”

我会坚持使用 $.ajax

$.getJSON("http://localhost:1505/getServiceImageList?data=yes&callback=?", loadImagesInSelect);

function loadImagesInSelect(json) {
   //whatever you want on success
}

then server side with php (note: I added data to the GET query string)

$data = getDataAsJSON($_GET['data']);
echo $_GET['callback'] . '(' . $data . ');';

getJSON needs to see "callback=?"

I would stick with $.ajax

記憶穿過時間隧道 2024-09-04 01:13:11

据我了解,您需要使用

$.getJSON("http://localhost:1505/getServiceImageList?callback=?", loadImagesInSelect);

jQuery 来为回调命名,然后将调用转发给 loadImagesInSelect

我希望这会有所帮助
杰罗姆·瓦格纳

from what I understand you need to use

$.getJSON("http://localhost:1505/getServiceImageList?callback=?", loadImagesInSelect);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文