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

后端 未结 3 489
时光取名叫无心
时光取名叫无心 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:09

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

    First, Javascript in document:

    
    

    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.

提交回复
热议问题