how to pass a javascript code in jsonp format and

2020-07-24 21:40发布

问题:

Can we use jsonp to overcome same domain policy of JS.

I need to run script from a domain x to run on domain y. So is it possible to send a script and execute ??

回答1:

Yes, that is the entire point of JSONP.

There is no restriction on where you can load a script from (other then the usual http/https conflicts).



回答2:

You can import a JS/CSS file from any other domain.

If you need to GET data from some other domain, you will need to get it through JSONP.

Please note that cross domain requests work only for HTTP/S GET and the only format of data accepted is JSONP.

e.g.

my code using jquery

$.ajax({
            url: 'https://www.otherDomain.com',
            type: "GET",
            crossDomain: true,
            data: parameters,
            dataType: "jsonp",
            jsonpCallback: "localJsonpCallback"
        });


function localJsonpCallback(json) {
 /* Do stuff */
}

The server side response needs to be in JSONP.