浅谈jsopn跨域请求原理及cors(跨域资源共享)解决方案
来源:互联网 发布:网络打击赌博最新新闻 编辑:程序博客网 时间:2024/06/18 10:55
由于同源策略的缘故,ajax不能向不同域的网站发出请求。
比如a站localhost需要向b站请求数据,地址为:http://www.walk-sing.com/api.php
请求的代码如下:
<html><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><script type="text/javascript">$.get("http://www.walk-sing.com/api.php", function(data){ alert("Data Loaded: " + data);});</script><body></body></html>
访问该页面,页面空白,按F12打开控制台,可以看到截图所示信息:
解决方案1:jsonp
我们先来看这样一个例子:
<html><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><script type="text/javascript">function alertSomething(data){alert(data.name+data.age);}alertSomething({"name":"ben","age":24});// $.get("http://www.walk-sing.com/api.php", function(data){// alert("Data Loaded: " + data);// });</script><body></body></html>执行结果:
我们也可以这样写:
<html><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><script type="text/javascript">function alertSomething(data){alert(data.name+data.age);};// $.get("http://www.walk-sing.com/api.php", function(data){// alert("Data Loaded: " + data);//});</script><script type="text/javascript" src="alertsomething.js"></script><body></body></html>
alertsomething.js的内容如下:
alertSomething({"name":"ben","age":24});
也可以得到截图所示结果。
我们再换一个方式,将alertsomething.js上传到服务器,将代码改为如下形式:
<html><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><script type="text/javascript">function alertSomething(data){alert(data.name+data.age);};// $.get("http://www.walk-sing.com/api.php", function(data){// alert("Data Loaded: " + data);//});</script><script type="text/javascript" src="http://www.walk-sing.com/alertsomething.js"></script><body></body></html>
也可以得到截图所示结果。
解决方案1:jsonp
不知道大家发现没有,script标签没有同源策略的限制,jsonp正是基于此原理实现的。
jsonp的具体实现可参见如下代码:
jsonp.php
<?php$method = isset($_GET['callback']) ? $_GET['callback'] : '';if(!isset($method)){exit('bad request');}$testArr = array('name' => 'ben','age' => 23);echo $method.'('.json_encode($testArr).')';
js代码:
<html><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><script type="text/javascript">function alertSomething(data){alert(data.name+data.age);};// $.get("http://www.walk-sing.com/api.php", function(data){// alert("Data Loaded: " + data);//});</script><script type="text/javascript" src="http://www.walk-sing.com/jsonp.php?callback=alertSomething"></script><body></body></html>
也可以得到截图所示结果。
解决方案二:CORS(跨域资源共享,Cross-Origin Resource Sharing)
不知道大家发现了没有,jsonp只能发送get请求,而如果业务中需要用到post请求时,jsonp就无能为力了。
这时候cors(跨域资源共享,Cross-Origin Resource Sharing)就派上用处了。
CORS的原理:
CORS定义一种跨域访问的机制,可以让AJAX实现跨域访问。CORS 允许一个域上的网络应用向另一个域提交跨域 AJAX 请求。实现此功能非常简单,只需由服务器发送一个响应标头即可。
就拿前面第一个例子来说,我只要在api.php文件头加上如下一句话即可:
header('access-control-allow-origin:*');
再次请求该接口,结果如下截图所示:
0 0
- 浅谈jsopn跨域请求原理及cors(跨域资源共享)解决方案
- 跨域资源共享(CORS)
- CORS(跨域资源共享)
- 跨域资源共享(CORS)
- 跨域资源共享CORS
- CORS 跨域资源共享
- 跨域资源共享 CORS
- 跨域资源共享cors
- CORS跨域资源共享
- 跨域资源共享 CORS
- 了解CORS(跨域资源共享)
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享)简介。。。
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享) 的配置
- CORS(跨域资源共享) 的配置
- bzoj-3529 数表
- [LeetCode]Intersection of Two Linked Lists
- java学习笔记(二) InputStream
- Karatsuba Multiplication
- 我的Android进阶之旅------>四种呼叫转移场景
- 浅谈jsopn跨域请求原理及cors(跨域资源共享)解决方案
- 参数类型对java用继承实现多态时的影响
- hdu 5285 wyh2000 and pupil
- Android问题集锦之二十八:You need to use a Theme.AppCompat theme (or descendant) with this activity.
- UEFI简单的应用程序模块MyHelloWorld:C源文件/INF工程文件源码/简析[6]
- ACM--Entrophy(优先队列建树+哈希)
- JAVA爬虫WebCollector
- Java数据结构-线性表之单链表LinkedList
- finally代码块中的代码什么时候执行