Jsonp_javascript simulation in JavaScript

Source: Internet
Author: User
This article describes how to simulate jsonp in JavaScript. This article provides the implementation code directly. the code contains detailed comments. For more information, see
Function prescript (s) {if (s. cache === undefined) {s. cache = false;} if (s. crossDomain) {s. type = "GET" ;}} function prejsonp (s, originalSettings, jqXHR) {// name var callbackName = s for the callback function. jsonpCallback s. url + = (/(? :)/. Test (s. url )? "&":"? ") + S. jsonp + "=" + callbackName; // after the script is executed, use a data converter to retrieve json. // The data that is provided to the code to obtain the server is s. getData = function () {if (! ResponseContainer) {jQuery. error (callbackName + "was not called");} return responseContainer [0];}; // modify the processing mechanism s. dataTypes [0] = "json"; // create a global function overwritten = window [callbackName]; // used to collect data from the server window [callbackName] = function () {responseContainer = arguments;}; return "script";}/*** jsonp pre-processing */function inspectPrefiltersOrTransportsA (options, originalOptions, jqXHR) {// pre-process jsonp var dataTypeOrTransport = prejsonp (options, originalOptions, jqXHR) // expand dataTypes options. dataTypes. unshift (dataTypeOrTransport); // pre-processing script type prescript (options)}/*** distributor * @ return {[type]} [description] */function inspectPrefiltersOrTransportsB (s, originalOptions, jqXHR) {return {send: function (_, complete) {var script = jQuery ("script"). prop ({async: true, charset: s. scriptCharset, src: s. url }). on ("load error", callback = function (evt) {script. remove (); callback = null; if (evt) {complete ()}});//

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.