【问题标题】:Can I make a jQuery JSONP request without adding the '?callback=' parameter in URL?我可以在 URL 中不添加 '?callback=' 参数的情况下发出 jQuery JSONP 请求吗?
【发布时间】:2012-10-03 13:34:57
【问题描述】:

服务器不会接受请求 URL 中的任何参数,所以我需要删除 URL 中所有额外的参数,当然我无法控制服务器。

jQuery:

$.ajax({
    type: 'GET',
    url: 'http://cross-domain.com/the_jsonp_file,
    jsonpCallback: 'jsonCallback',
    contentType: 'application/json',
    cache: 'true',
    dataType: 'jsonp',
    success: function(json) {
        console.log(json);
    },
});

JSONP 文件:

jsonCallback({"test": "hello"});

当我发送 Ajax 请求时,URL 如下所示:

http://cross-domain.com/the_jsonp_file?callback=jsonCallback

但我需要这个(不带参数):

http://cross-domain.com/the_jsonp_file

编辑:

这是我的全部情况:

function MyClass(imgs) {
    // imgs is array of URLs
    this.imgs = imgs;

    this.submit = function() {
        // button click event triggers this method
        this._show();
    };

    this._show = function() {
        var _this = this;

        for (var i = 0; i < _this.imgs.length; i++) {
            (function($, j) {
                $.ajax({
                    type: 'GET',
                    url: _this.imgs[j],
                    jsonp : false,
                    jsonpCallback: 'jsonCallback',
                    cache: 'true',
                    dataType:'jsonp',
                    success: function(json) {
                      console.log(_this.imgs[j]);
                    },
                });
            })(jQuery, i);
        };
    };
};

我收到了这个错误信息:

Uncaught TypeError: Property 'jsonCallback' of object [object Window] is not a function

奇怪的是很少有请求成功调用 jsonCallback。

【问题讨论】:

  • 服务器完全拒绝带有查询字符串的请求?
  • 您最好自己编写 JSONP 代码,而不是使用 jQuery...
  • 我在 ajax 设置中尝试了dataType: 'script',它正在制作没有任何参数的 URL,但我不知道如何使回调函数工作。
  • 我不明白您希望如何在不指定回调的情况下执行 JSONP。它是一个脚本标签,传递回调名称的唯一方法是通过 URL。我错过了什么?也许它将它包装在默认回调中?网址是什么?

标签: javascript jquery ajax cross-domain jsonp


【解决方案1】:

查看 jQuery 文档 - 他们说 jsonp: false 和 jsonpCallback : ajax args 中的 'callbackFunction' ....like:

$.ajax({
    url: 'http://cross-domain.com/the_jsonp_file',
    jsonp : false,
    jsonpCallback: 'jsonCallback',
    // contentType: 'application/json', -- you can't set content type for a <script> tag, this option does nothing for jsonp | KevinB
    cache: 'true',
    dataType : 'jsonp'
});

http://api.jquery.com/jQuery.ajax/

【讨论】:

  • 谢谢。它似乎有效,但我收到此错误消息:Uncaught TypeError: Property 'jsonCallback' of object [object Window] is not a function。少数元素成功调用回调函数。
  • @Jonypoins 你在使用 jQuery 1.5+ 吗?
  • 那是因为你还没有定义一个名为 jsonCallback 的函数。 jsonpCallback 属性的值需要是一个全局可用的函数,才能在返回响应时执行。
【解决方案2】:

每个请求都会调用相同的回调jsonCallback,所以我认为这就是问题所在。

首先,文档中的Javascript:

<script type="text/javascript">
    new Gallery([
        ['http://cross-domain.url/whatever', '27b2afa5c77c2510'],
        ['http://cross-domain.url/whatever', '13df51b2f2801bc1'],
        ['http://cross-domain.url/whatever', '4de326fc9a2c5a24'],
        ['http://cross-domain.url/whatever', '60c266a73ba699bc'],
        ['http://cross-domain.url/whatever', '3db01e95aaf2b9f2'],
        ['http://cross-domain.url/whatever', '94eb17f9b0e1be9c'],
        ['http://cross-domain.url/whatever', 'ca8c5c3c0b8cd5ae'],
        ['http://cross-domain.url/whatever', '6b0f5c5737ee88fd'],
        ['http://cross-domain.url/whatever', '318d8ebb51a97a15'],
        ['http://cross-domain.url/whatever', 'f5028c8b62e81a8b'],
    ]);
</script>

客户端将 JSONP 文件(只是另一个 Javascript 文件)上传到服务器,如下所示:

jsonCallback_27b2afa5c77c2510({"test": "hello"});

jsonCallback_ 之后添加随机十六进制字符串来分隔每个请求,就像 jQuery 的默认回调一样。

从输入中读取随机十六进制字符串并设置为jsonpCallback

function Gallery(imgs) {
    // imgs is array of URLs
    this.imgs = imgs;

    this.submit = function() {
        // button click event triggers this method
        this._show();
    };

    this._show = function() {
        var _this = this;

        for (var i = 0; i < _this.imgs.length; i++) {
            (function($, j) {
                $.ajax({
                    type: 'GET',
                    url: _this.imgs[j][0],
                    jsonp : false,
                    jsonpCallback: 'jsonCallback_' + _this.imgs[j][1],
                    cache: 'true',
                    dataType:'jsonp',
                    success: function(json) {
                      // Process
                      console.log(json.test);
                    },
                });
            })(jQuery, i);
        };
    };
};

谢谢@Adam @Kevin B @Dcullen 和大家! :D

p.s:我输入上面的每个来源只是举例,它可能不正确。

【讨论】:

    【解决方案3】:

    JSONP 需要回调作为 URL 的一部分。根据您访问的服务器不支持JSONP的描述,我猜想。

    jQuery 将脚本标签添加到您的文档中,添加该标签后会运行 API 请求,响应会调用代码中的回调函数(这是为了简化它)。

    如果您想要更准确的描述,可以查看 Wikipedia。 http://en.wikipedia.org/wiki/JSONP#How_it_works

    【讨论】:

    • 是的,服务器不支持 JSONP 但我想通过 JSONP 使用来自服务器的资源。
    • 不幸的是,您的服务器必须支持 JSONP 才能正常工作。否则你会得到上面提到的脚本错误。您还有 2 个其他选项: 1. 设置代理。 2. CORS(这也需要服务器配置)
    • "每个请求都会调用同一个回调 jsonCallback,所以我认为这就是问题所在。" - 这是我的问题。谢谢。 +1
    猜你喜欢
    • 2011-09-02
    • 2018-03-09
    • 2016-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2014-07-13
    • 2021-12-27
    相关资源
    最近更新 更多