how to pass a javascript code in jsonp format and execute

女生的网名这么多〃 提交于 2019-12-12 09:00:59

问题


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.



来源:https://stackoverflow.com/questions/22627773/how-to-pass-a-javascript-code-in-jsonp-format-and-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!