How to make a JSONP request from Javascript without JQuery?

后端 未结 12 1424
遇见更好的自我
遇见更好的自我 2020-11-22 17:27

Can I make a cross-domain JSONP request in JavaScript without using jQuery or other external library? I would like to use JavaScript itself and then parse the data and make

12条回答
  •  不知归路
    2020-11-22 17:43

    My understanding is that you actually use script tags with JSONP, sooo...

    The first step is to create your function that will handle the JSON:

    function hooray(json) {
        // dealin wit teh jsonz
    }
    

    Make sure that this function is accessible on a global level.

    Next, add a script element to the DOM:

    var script = document.createElement('script');
    script.src = 'http://domain.com/?function=hooray';
    document.body.appendChild(script);
    

    The script will load the JavaScript that the API provider builds, and execute it.

提交回复
热议问题