Deep source analysis uses jquery to continuously initiate JSONP request failure reasons

Source: Internet
Author: User

The Jsonp of jquery should be very familiar to everyone.
Have encountered such a demand
1, want to request a few similar content to add to the page
2, the content of the request for a certain period of time is fixed, want to do a cache.

So the brain shot and wrote a code like this.

 for (var i = 0; i < 3; i++) {    $.ajax ({        URL:' .../return.php?num= ' +I,        ' Jsonp ',         ' callback ',        ' dosome ',        true    }). Done (  function(re) {        console.log (re);    }). Fail (function() {        console.log (' fail ');    });

The result is always only one success and error

function

Baffled, not a success? Why is dosome not a function?
Helpless under the great thought and time on the localhost research on the JSONP principle of jquery.

The settings server returns the following

<? PHP      Echo ' Dosome (' num= '.  $_get[' num ']. ' "); ' ;? >

Get returned as follows


Look through the source code, found in 1.11.3 version

For each JSONP request, jquery automatically registers the Callbackname function with the window and returns the window[Callbackname] back.

Then after the synchronous execution of the For loop send request, processing the first return when the window[Callbackname] changed to undefined, the subsequent return can not be processed.

I was depressed, anyway, this function did not do anything, do not change back?

Unfortunately, I am still too naïve, in fact, do not change back also can not normally get the desired results.

Personally, the JSONP principle of jquery is roughly as follows

Each time a JSONP request is made, a new handler is created to assign the return content to the local variable Responsecontainer, which is then executed in the call to the registered callback function with the corresponding local variable responsecontainer[0] for the parameter.

When you use a different handler name, everything is fine (when we don't write Jsonpcallback, jquery automatically generates the only different function names). Just like the dosome1,2,3 above, each reference and process.

When using the same function name, the loop window[' dosome ' order is assigned, eventually pointing to the last processing function (the middle red Line), and the others are recycled. Executes on the first return and assigns the contents to the last local variable.

In this way, the first request will fail to get the returned content, and the last callback of the request handles the content that is not the request.

Deep source analysis uses jquery to continuously initiate JSONP request failure reasons

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.