Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jsonp跨域问题原理解析大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

JavaScript是一种在Web开发中经常使用的前端动态脚本技术。在JavaScript中,有一个很重要的安全性限制,被称为“Same-Origin Policy”(同源策略)。这一策略对于JavaScript代码能够访问的@L_197_2@内容做了很重要的限制,即JavaScript只能访问与包含它的文档在同一域下的内容

JavaScript这个安全策略在进行多iframe或多窗口编程、以及Ajax编程时显得尤为重要。根据这个策略,在baidu.com下的@L_197_2@中包含的JavaScript代码,不能访问在google.com域名下的@L_197_2@内容;甚至不同的子域名之间的@L_197_2@也不能通过JavaScript代码互相访问。对于Ajax的影响在于,通过XMLhttprequest实现的Ajax请求,不能向不同的域提交请求,例如,在abc.example.com下的@L_197_2@,不能向def.example.com提交Ajax请求,等等。

然而,当进行一些比较深入的前端编程的时候,不可避免地需要进行跨域操作,这时候“同源策略”就显得过于苛刻。JSONP跨域GET请求是一个常用的解决方案,下面我们来看一下JSONP跨域是如何实现的,并且探讨下JSONP跨域的原理。

利用在@L_197_2@中创建<script>节点的方法向不同域提交http请求的方法称为JSONP,这项技术可以解决跨域提交Ajax请求的问题。JSONP的工作原理如下所述:

假设在http://example1.com/index.PHP这个@L_197_2@中向http://example2.com/geTinfo.PHP提交GET请求,我们可以将下面的JavaScript代码放在http://example1.com/index.PHP这个@L_197_2@中来实现:

1 vareleScript= document.createElement("script");
2
eleScript.type ="text/javascript";
3
eleScript.src ="http://example2.com/getinfo.php";
4 document.getElementsByTagName("HEAD")[0].appendChild(eleScript);

当GET请求从http://example2.com/geTinfo.PHP返回时,可以返回一段JavaScript代码,这段代码自动执行,可以用来负责调用http://example1.com/index.PHP@L_197_2@中的一个callBACk函数

JSONP的优点是:它不像XMLhttprequest对象实现的Ajax请求那样受到同源策略的限制;它的兼容性更好,在更加古老的浏览器中都可以运行,不需要XMLhttprequest或ActiveX的支持;并且在请求完毕后可以通过调用callBACk的方式回传结果。

JSONP的缺点则是:它只支持GET请求而不支持POST等其它类型的http请求;它只支持跨域http请求这种情况,不能解决不同域的两个@L_197_2@之间如何进行JavaScript调用的问题。

再来一个例子:

01 qsData = {'searchWord':$("#searchWord").attr("value"),'currentUserId':
02 $("#currentUserId"'conditionBean.pageSize'"#pageSize")};
03
04 $.ajax({
05
async:false,
06 url: http://跨域的dns/document!searchJSONResult.action,
07
type:"GET"08 dataType:'jsonp'09 jsonp:'jsoncallBACk'10 data: qsData,136); width:171px; margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; padding:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; line-height:1.1em!important; font-family:'Courier New',monospace!important; width:2.7em!important; margin:0px!important; padding:0px 0.3em 0px 0px!important; border:0px!important; outline:0px!important; text-align:right!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; line-height:1.1em!important; min-height:inherit!important; display:block!important; BACkground:none!important">11 timeout: 5000,136); width:234px; margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; padding:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; line-height:1.1em!important; font-family:'Courier New',monospace!important; width:2.7em!important; margin:0px!important; padding:0px 0.3em 0px 0px!important; border:0px!important; outline:0px!important; text-align:right!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; line-height:1.1em!important; min-height:inherit!important; display:block!important; BACkground:none!important">12 beforeSend:function(){
@H_502_226@13 //jsonp 方式此方法不被触发.原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了
14 },monospace!important; width:2.7em!important; margin:0px!important; padding:0px 0.3em 0px 0px!important; border:0px!important; outline:0px!important; text-align:right!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; line-height:1.1em!important; min-height:inherit!important; display:block!important; BACkground:none!important">15 success:(json) {//客户端jquery预先定义好的callBACk函数,成功获取跨域服务器上的json数据后,会动态执行这个callBACk函数
16 if(json.actionErrors.length!=0){
17
alert(json.actionErrors);
18 }
19
gendynamicContent(qsData,type,json);
@H_233_301@20 21 complete:(XMLhttprequest,textStatus){

大佬总结

以上是大佬教程为你收集整理的jsonp跨域问题原理解析全部内容,希望文章能够帮你解决jsonp跨域问题原理解析所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。