Limitations of using JSONP in jquery Ajax

Source: Internet
Author: User
Tags error handling

JSONP solves the problem of Cross-domain Ajax invocation. Why cross Domain Ajax calls? This can be in an application directly in front of the front-end through JS invoke another application (under different domain name) API.

We also use the JSONP in the actual application, but only then knew only one limit of JSONP, can only send GET request, the flaw of the request is the limitation of the length of requests.

Today, the discovery of another limitation of JSONP (in the jquery Ajax scenario)-does not trigger $.ajax's error callback, as follows:

$.ajax ({
    dataType: ' Jsonp ',            
    error:function (XHR) {
        //error does not execute this callback function
    }
});

This restriction is determined by the JSONP implementation mechanism.

Find two pieces of information on the internet about this issue:

[JQuery]. Ajax () with DataType: ' Jsonp ' won't use error callback if request fails

JSONP error handling with Jquery.ajax

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/ajax/

Workaround:

Using a jquery plugin--jquery-jsonp,https://github.com/jaubourg/jquery-jsonp

Sample code:

<script src= "Https://raw.github.com/jaubourg/jquery-jsonp/master/src/jquery.jsonp.js" ></script>
$.jsonp ({
    URL: ',
    success:function (data) {
    },
    error:function (xoptions, textstatus) {
        Console.log (Textstatus);
    }
);

When a JSONP request fails, such as a 404 error, the error callback function executes, and the output string "error".

Related Article

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.