如何以jsonp格式传递javascript代码并执行 [英] how to pass a javascript code in jsonp format and execute

查看:61
本文介绍了如何以jsonp格式传递javascript代码并执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用jsonp来克服JS的相同域策略吗?

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

我需要从域x运行脚本以在域y上运行。那么是否可以发送脚本并执行??

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

推荐答案

您可以从任何其他域导入JS / CSS文件。

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

如果您需要从其他域获取数据,则需要通过JSONP获取数据。

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

请注意,跨域请求仅适用于HTTP / S GET和接受的唯一数据格式是JSONP。

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

例如

我的代码使用jquery

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 */
}

服务器端响应需要在JSONP中。

The server side response needs to be in JSONP.

这篇关于如何以jsonp格式传递javascript代码并执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆