探讨跨域请求资源的几种方式(总结)

跨域请求资源的几种方式,具体如下:

1.什么是跨域

2.JSONP

3.proxy代理

4.cors

5.xdr

由于浏览器同源策略,凡是发送请求url的协议、域名、端口三者之间任意一与当前页面地址不同即为跨域。具体可以查看下表

探讨跨域请求资源的几种方式(总结)

JSONP

这种方式主要是通过动态插入一个script标签。浏览器对script的资源引用没有同源限制,同时资源加载到页面后会立即执行(没有阻塞的情况下)。

<script> var _script = document.createElement('script'); _script.type = "text/javascript"; _script.src = "http://localhost:8888/jsonp?callback=f"; document.head.appendChild(_script); </script>

实际项目中JSONP通常用来获取json格式数据,这时前后端通常约定一个参数callback,该参数的值,就是处理返回数据的函数名称。

<!doctype html> <html> <head> <meta charset="utf-8"> <meta content="initial-scale=1, maximum-scale=1,user-scalable=no"> <title>jsonp_test</title> <script> var f = function(data){ alert(data.name); } /*var xhr = new XMLHttpRequest(); xhr.onload = function(){ alert(xhr.responseText); }; xhr.open('POST', 'http://localhost:8888/cors', true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("f=json");*/ </script> <script> var _script = document.createElement('script'); _script.type = "text/javascript"; _script.src = "http://localhost:8888/jsonp?callback=f"; document.head.appendChild(_script); </script> </head> var query = _url.query; console.log(query); var params = qs.parse(query); console.log(params); var f = ""; f = params.callback; res.writeHead(200, {"Content-Type": "text/javascript"}); res.write(f + "({name:'hello world'})"); res.end();

探讨跨域请求资源的几种方式(总结)

缺点:

1、这种方式无法发送post请求(这里)

2、另外要确定jsonp的请求是否失败并不容易,大多数框架的实现都是结合超时时间来判定。

Proxy代理

这种方式首先将请求发送给后台服务器,通过服务器来发送请求,然后将请求的结果传递给前端。

<!doctype html> <html> <head> <meta charset="utf-8"> <meta content="initial-scale=1, maximum-scale=1,user-scalable=no"> <title>proxy_test</title> <script> var f = function(data){ alert(data.name); } var xhr = new XMLHttpRequest(); xhr.onload = function(){ alert(xhr.responseText); }; xhr.open('POST', 'http://localhost:8888/proxy?', true); xhr.send("f=json"); </script> </head> <body> </body> </html>

var proxyUrl = ""; if (req.url.indexOf('?') > -1) { proxyUrl = req.url.substr(req.url.indexOf('?') + 1); console.log(proxyUrl); } if (req.method === 'GET') { request.get(proxyUrl).pipe(res); } else if (req.method === 'POST') { var post = ''; //定义了一个post变量,用于暂存请求体的信息 req.on('data', function(chunk){ //通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中 post += chunk; }); req.on('end', function(){ //在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。 post = qs.parse(post); request({ method: 'POST', url: proxyUrl, form: post }).pipe(res); }); }

探讨跨域请求资源的几种方式(总结)

需要注意的是如果你代理的是https协议的请求,那么你的proxy首先需要信任该证书(尤其是自定义证书)或者忽略证书检查,否则你的请求无法成功。12306就提供了一个鲜活的例子。

探讨跨域请求资源的几种方式(总结)

探讨跨域请求资源的几种方式(总结)

还需要注意一点,对于同一请求浏览器通常会从缓存中读取数据,我们有时候不想从缓存中读取,所以会加一个preventCache参数,这个时候请求url变成:url?preventCache=12345567....;这本身没有什么问题,问题出在当使用某些前端框架(比如jquery)发送proxy代理请求时,请求url为proxy?url,同时设置preventCache:true,框架不能正确处理这个参数,结果发出去的请求变成proxy?url&preventCache=123456(正长应为proxy?url?preventCache=12356);后端截取后发送的请求为url&preventCache=123456,根本没有这个地址,所以你得不到正确结果。

CORS

这是现代浏览器支持跨域资源请求的一种方式。

探讨跨域请求资源的几种方式(总结)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwxwyd.html