Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the underscore for in the query string of a jQuery jsonp ajax request?

Tags:

jquery

jsonp

When I look at the query string from a jsonp request (client code below), there are 2 objects, a "callback" string that you need to use in the response (so the client codes directs to the success handler) and one with a key of _... what is this underscore for? I can't find any reference to this in any documentation, it appears to be a number of some kind.

I though that it might be used to direct to the error handler (either on its on, in combination with the callback, or replacing the number after the underscore in the callback string), but it doesn't appear to be.

url = 'http://localhost:11767/Handlers/MyHandler.ashx';

...

$.ajax({
    url: url,
    dataType: "jsonp",
    error: function (jqXHR, textStatus, errorThrown) {
        //...
    },
    success : function(d) {
        //...
    }
});

or

    $.getJSON(url + "?callback=?", function(d) {
    }).success(function(d) {
        //...
    }).error(function(jqXHR, textStatus, errorThrown) {
        //...
    }).complete(function(d) {
        //...
    });

Side note in case this helps anyone reading this: as this is a jsonp request, error will only be hit if the exception occurs client side, e.g. there is a timeout or a problem with the formatting of response (i.e. not using the callback), to overcome this, I always log and swallow the exceptions in the handlers, but give a standard response object (which all response are made up of) that has a state property of exception and a message property.

like image 349
Mr Shoubs Avatar asked Oct 03 '12 13:10

Mr Shoubs


People also ask

What is AJAX in jQuery?

What is AJAX? AJAX = Asynchronous JavaScript and XML. In short; AJAX is about loading data in the background and display it on the webpage, without reloading the whole page. Examples of applications using AJAX: Gmail, Google Maps, Youtube, and Facebook tabs. You can learn more about AJAX in our AJAX tutorial.

What is AJAX request in JavaScript?

What's AJAX? AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.

What is content type in AJAX?

contentType is the type of data you're sending, so application/json; charset=utf-8 is a common one, as is application/x-www-form-urlencoded; charset=UTF-8 , which is the default. dataType is what you're expecting back from the server: json , html , text , etc.


1 Answers

The number you are referring is the date time stamp of the request. Grab the number and use a your browser's JavaScript console and type: alert(new Date(/*insert number here*/))

You'll get an alert with a date/time.

EDIT:

Here's a snippet from jQuery.ajax doc regarding an ajax request:

cache
Default: true, false for dataType 'script' and 'jsonp'
If set to false, it will force requested pages not to be cached by the browser. Setting cache to false also appends a query string parameter, "_=[TIMESTAMP]", to the URL.

like image 186
Justin Self Avatar answered Nov 05 '22 11:11

Justin Self