Can I make a jQuery JSONP request without adding the '?callback=' parameter in URL?

后端 未结 3 475
时光取名叫无心
时光取名叫无心 2020-12-10 15:39

The server won\'t accept any parameters in a request URL, so I need to remove all the extra parameters in the URL and of course I can\'t control the server.

jQuery:<

相关标签:
3条回答
  • 2020-12-10 16:02

    JSONP requires a callback as part of the URL. I would guess based on the description that the server you are accessing does not support JSONP.

    jQuery adds a script tag to your document, which when added runs the API request, the response calls the callback function in your code (this is simplifying it).

    You can take a look at Wikipedia if you want a more accurate description. http://en.wikipedia.org/wiki/JSONP#How_it_works

    0 讨论(0)
  • 2020-12-10 16:09

    Check the jQuery docs - they say to say jsonp: false and jsonpCallback : 'callbackFunction' in the ajax args....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/

    0 讨论(0)
  • 2020-12-10 16:09

    Every requests calls the same callback jsonCallback, so I thought that's the problem.

    First, Javascript in document:

    <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>
    

    Client uploads JSONP file(just another Javascript file) to the server like this:

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

    Added random hex string after jsonCallback_ to separate each requests like jQuery's default callback does.

    Read random hex string from input and set as 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);
            };
        };
    };
    

    Thank you @Adam @Kevin B @Dcullen and everyone! :D

    p.s: I typed every sources above only for example, it may not correct.

    0 讨论(0)
提交回复
热议问题