XMLHttpRequest cannot load问题分析

1,139 阅读3分钟

Offer 驾到,掘友接招!我正在参与2022春招打卡活动,点击查看活动详情

我们在做项目的时候经常会遇到 错误信息:

XMLHttpRequest cannot load web.image.myqcloud.com/photos/v2/1… 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

分析:

Access control allow origin直译过来就是"访问控制允许同源",这是由于ajax跨域访问引起的。所谓跨域就是,在a.com域下,访问b.com域下的资源;出于安全的考虑,浏览器允许跨域写,而不允许跨域读,写就是上行,发送请求,send request,读就是下行,接受响应,receive response;理解了这两条规则,以下现象我们也就能理解了;

 

1、表单默认提交(get、post)、超链接访问域外的资源,这是允许的,因为在点击按钮/超链接时,浏览器地址已经变了,这就是一个普通的请求,不存在跨域;

2、ajax(借助xmlhttprequest)跨域请求,这是被禁止的,因为ajax就是为了接受接受响应,这违背了,不允许跨域读的原则

3、jsonp属于跨域读且形式限制为GET方式,它利用了script标签的特性;这是允许的。因为浏览器把跨域读脚本,当作例外,类似的img、iframe的src都可以请求域外资源

解决办法:

a,

非IE浏览器,利用xmlhttprequest,在request header添加origin:本域名地址,通常不需要手动添加,浏览器会动添加

IE浏览器,利用XDomainRequest发送请求,它会自动封装header中添加origin

b,response header添加access-control-allow-origin:*

 

跨域演示及代码

验证过程,首先访问http://本机ip:port/project_name/a.jsp,然后,a.jsp发送ajax请求,http://localhost:port/project_name/b.jsp,这样就产生了跨域的问题。

a.jsp

  <%@ page language="java" contentType="text/html; charset=UTF-8"  
      pageEncoding="UTF-8"%>  
  <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">  
  <head>  
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>  
  <title>跨域演示</title>  
  <script type="text/javascript" src="jquery-1.11.2.js"></script>  
  <script type="text/javascript" src="jquery.form.js"></script>  
  </head>  
    
  <script type="text/javascript">  
  $(document).ready(function() {  
        
      $.getJSON('http://localhost/test_maven/b.jsp'function(data) {  
          alert("request succeed");  
      });  
  });  
  </script>  
  </body>

b.jsp

  <%@ page language="java" contentType="text/html; charset=UTF-8"  
      pageEncoding="UTF-8"%>  
  <%  
    
  response.setHeader("Access-Control-Allow-Origin", "*");  
  response.getWriter().write("hahahhaha");  
  %>

如果注释掉response.setHeader("Access-Control-Allow-Origin", "");,那么就会出现access control allow origin错误;使用通配符,允许所有跨域访问,所以跨域访问成功。

但是,请注意使用通配符*,会允许来自任意域的跨域请求访问成功,这是比较危险的,所以在生产环境通常会做更精确控制;

简单请求

上述请求是简单请求,只要添加了Access-Control-Allow-Origin:*,就会访问成功,如果是复杂请求,我们需要更进一步的处理,才能成功。这里先解释一下什么是简单请求和复杂请求。

 Simple requests  
    
  A simple cross-site request is one that meets all the following conditions:  
    
  The only allowed methods are:  
  GET  
  HEAD  
  POST  
  Apart from the headers set automatically by the user agent (e.g. Connection, User-Agent, etc.), the only headers which are allowed to be manually set are:  
  Accept  
  Accept-Language  
  Content-Language  
  Content-Type  
  The only allowed values for the Content-Type header are:  
  application/x-www-form-urlencoded  
  multipart/form-data  
  text/plain

简单来说,符合上述条件的都是“简单请求”,除了简单请求就是“复杂请求”。 复杂请求

在正式post之前,浏览器会先发出一个options请求(也叫preflight),同时header带上origin还有Access-Control-Request-*:**之类的头,服务器响应会返回相应的access-control-allow-origin,如果匹配,那么浏览器就会发送正式post,否则就会出现上述错误。这也解答了,跨域访问时,我们明明发送的post请求,失败的话,查看chrome network会发现是options方法的原因。

总结:

出现Access control allow origin错误,说明是跨域请求失败!浏览器发送请求成功,同时浏览器也接收到响应了,但是限制了XmlHttpRquest接收请求,不会让xmlhttprequest接受到响应,并且在js控制台报错。这也就是我们在网络控制台(Network)能看见http 状态码是200,但是在js控制台(Console)出现js错误的原因。