浏览器跨域小记

同源政策规定,AJAX请求只能发给同源的网址,否则就报错。

这里写图片描述
* 1. 跨域问题只存在浏览器中,服务器端跨域请求是不存在问题的
* 2. JSONP只支持GET请求
* 3. CORS支持所有类型的HTTP请求,需要在服务器的白名单
* 4. x-requested-with XMLHttpRequest //表明是AJax异步
* 5. 跨域关键,请求头Origin,谷歌插件可测试 RESTful API
* 6. 在别人家网站测试,可以F12打开控制台中测试练习 =。= 污
* 7. 代理服务,扔到服务器上运行,然后拿结果

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <input type="button" value="测试喽" onclick="ajax();" />
  <script type="text/javascript">
    function ajax() {
      var xhr = new XMLHttpRequest();
      xhr.open("POST", "http://www.runoob.com/try/ajax/demo_post.php", true);
      xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
      xhr.send("id=2");
      xhr.onreadystatechange = function doResult() {
        if (xhr.readyState == 4) {//4代表执行完成
          if (xhr.status == 200) {
            document.getElementById("resText").innerHTML = xhr.responseText;
          }
        }
      }
    }
  </script>
</body>
</html>

这里写图片描述

参考链接:http://www.ruanyifeng.com/blog/2016/04/same-origin-policy.html
http://www.ruanyifeng.com/blog/2016/04/cors.html
http://www.runoob.com/ajax/ajax-xmlhttprequest-send.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jigsaw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值