Techniques for simulating jsonp_javascript in JavaScript

Source: Internet
Author: User
function Prescript (s) {if (S.cache = = undefined) {S.cache = false;
    } if (s.crossdomain) {s.type = ' get '; } function Prejsonp (s, originalsettings, JQXHR) {//to the callback function named var callbackname = S.jsonpcallback S.url += (/(?:) /.test (S.url)? "&": "?"
    + S.jsonp + "=" + callbackname; Using a data converter to retrieve JSON//supplied to the code fetch server after the execution of the script is S.getdata = function () {if (!responsecontainer) {jquery.er
      Ror (Callbackname + "is not called");
    return responsecontainer[0];
    };
    Modify the processing mechanism s.datatypes[0] = "JSON";
    Create a global function overwritten = Window[callbackname];
    Used to collect server-given data window[callbackname] = function () {responsecontainer = arguments;

    };
  return "script"; 
    /** * JSONP pre-processing/function Inspectprefiltersortransportsa (options, Originaloptions, JQXHR) {//preprocessing JSONP var datatypeortransport = Prejsonp (options, Originaloptions, JQXHR)//EXTENSION Datatypes Options.dataTypes.unshift (Datatypeortransport); Preprocessing script type Prescript (options)}/** * Distributor * @return {[type]} [description]/function Inspectpref ILTERSORTRANSPORTSB (S, originaloptions, JQXHR) {return {Send:function (_, complete) {var script = Jqu ery ("<script>"). Prop ({async:true, Charset:s.scriptcharset, Src:s.url}). On
            ("Load Error", callback = function (evt) {script.remove ();
            callback = NULL;
        if (evt) {Complete ()}}); <script async= "" src= "http://192.168.1.113:8080/github/jQuery/jsonp.php Document.head.appendChild (script[0))
      ; 
   /** * Simulate Ajax JSONP Request * @param {[type]} options [description] * @return {[type]} [description]
      */function Createajax (options) {if (typeof url = = "Object") {options = URL;
    url = undefined;

  }  options = Options | |

    {};

    /** * parameter * jquery.ajaxsetup is the default parameter * @type {[type]}/var s = Jquery.ajaxsetup ({}, Options);
    Deferreds//asynchronous mechanism var deferred = jquery.deferred ();

    var completedeferred = jquery.callbacks ("Once Memory"); /** * Actual return Ajax Object * @type {Object} */var jqxhr = {}//Convert JQXHR object to Promise object, add complete, success, er
    Ror method Deferred.promise (JQXHR). Complete = Completedeferred.add;
    Alias jqxhr.success = Jqxhr.done;

    Jqxhr.error = Jqxhr.fail; S.datatypes = Jquery.trim (S.datatype | | "*"). toLowerCase (). Match (/(?:) /) ||

    [""];

    Pretreatment of Inspectprefiltersortransportsa (S, options, JQXHR);
    For (i in {success:1, error:1, complete:1}) {Jqxhr[i] (s[i));

    /** * Distributor */transport = INSPECTPREFILTERSORTRANSPORTSB (s, options, JQXHR);
    function done (status, Nativestatustext, responses, headers) {Console.log (S,s.getdata ())///Send request Transport.send (s, done);
  return JQXHR;
  function Show (data) {$ (' body '). Append (' <li> ' + data + ' </li> '); /** * Data Callback received * @return {[type]} [description]/function Flighthandler () {} $ ("#test"). Click (functi
    On () {//executing an Asynchronous HTTP (Ajax) request. var ajax = createajax ({url: ' http://192.168.1.113:8080/github/jQuery/jsonp.php ', data: {' action ': ' A Aron '},//The array of DataType: ' Jsonp ',//data type JSONP: ' Callback ',//specifying the callback function name, consistent with the server-side reception, and sending back the JSONP Callback:flighthandler, Success:function () {Show (' Local event Success ')}})

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.