Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

$.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);
    },
});

The JSONP file:

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

When I send that Ajax request, the URL looks like this:

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

But I need this (without parameters):

http://cross-domain.com/the_jsonp_file

EDIT:

Here is my whole situation:

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);
        };
    };
};

And I got this error message:

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

Weird thing is few requests are successfully calling jsonCallback.

like image 909
Jonypoins Avatar asked Oct 12 '12 17:10

Jonypoins


People also ask

What is callback in JSONP request?

Callback means that it will call the JavaScript function whose name is specified in the Callback parameter once the response is ready. In this article I will explain JSONP request with Callback example using JavaScript i.e. how to make a Cross Domain JSONP request with Callback using plain JavaScript without using jQuery.

How to implement JSONP in jQuery?

This is a simple process to implement jsonp in jquery. 1) In this example we are using JSONP to request a cross-origin domain in JQuery to get the data. For instance, we are using a callback URL from git hub to make this example work. This is a sample example for beginners to understand the jsonp concept in JQuery.

How to use JSON Web service with JavaScript script tag?

As soon as the Script Tag is attached to the page, it makes call to the JSON Web Service along with the Callback function’s name DisplayIP as Callback parameter. The JSON Web Service returns the response in the specified Callback JavaScript function DisplayIP, which accepts the JSON response and displays it in HTML Span element.

Which parameter is used to set the JSON file type?

2) datatype: This parameter is used to set the type. Here we are setting ‘application/JSON’ as the type of the file. This is also referred to as the content of the file. How JSONP works in jQuery? As of now, we know that jsonp is used to handle the JSON over the network.


1 Answers

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/

like image 168
Adam Avatar answered Nov 06 '22 00:11

Adam