how to pass a javascript code in jsonp format and

2020-07-24 21:03发布

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 ??

2条回答
▲ chillily
2楼-- · 2020-07-24 21:33

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).

查看更多
贪生不怕死
3楼-- · 2020-07-24 21:49

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.

查看更多
登录 后发表回答