如何解决此问题:跨域读取阻止(CORB)阻止了跨源响应

时间:2019-03-14 10:18:24

标签: jquery json ajax cross-origin-read-blocking

警告是:

jquery-1.9.1.js:8526跨域读取阻止(CORB)阻止了MIME类型为application / json的跨域响应https://www.metaweather.com/api/location/search/?query=lo。有关更多详细信息,请参见https://www.chromestatus.com/feature/5629709824032768

我的代码是:

<!DOCTYPE html>
<html>
<head>
  <title> Search API </title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
  </script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"> 
  </script> 
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
   <div class="container">
     <h2>API Search Whether</h2>    
     <div class="row">
        <div class="col-md-6">
            <input type="text" name="name" id="name" class="form-control">
        </div>
        <div class="col-md-2">              
            <button type="button" id="search" class="btn btn-primary btn-lg">Search</button>
        </div>
    </div>
    <br><br><br><br>
    <div class="row">
        <table class="table" border="2px">
            <thead>
                <tr style="color:blue; font-weight:bold;">
                    <td >Domain</td>
                    <td>Suffix</td>
                    <td>Expiry Date</td>
                    <td>Created At</td>
                    <td>Country</td>
                </tr>
            </thead>
            <tbody id="tbody">
            </tbody>
        </table>
    </div>
</div>  
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"> 
</script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<script type="text/javascript">
    $("#search").click(function () 
    {
        $("#tbody").html("");           
        var $this = $(this);
        var loadingText = '<i class="fa fa-circle-o-notch fa-spin"></i>Loading...';
        $("#search").attr("disabled", true);
        if ($(this).html() !== loadingText) {
            $this.data('original-text', $(this).html());
            $this.html(loadingText);
        }   
        var name = $('#name').val();        
        var i;
        $.ajax({
            url: "https://www.metaweather.com/api/location/search/?query="+name,
            dataType:'json',
            headers: function(xhr){
                xhr.setRequestHeader('x-xss-protection' ,'1; mode=block');
                xhr.setRequestHeader('Content-Language' ,'en');
                xhr.setRequestHeader('x-content-type-options', 'nosniff');
                xhr.setRequestHeader('strict-transport- security' , 'max-age=2592000; includeSubDomains');
                xhr.setRequestHeader('Vary' , 'Accept-Language, Cookie');
                xhr.setRequestHeader('Allow' , 'GET, HEAD,OPTIONS');
                xhr.setRequestHeader('x-frame-options' ,'DENY');
                xhr.setRequestHeader('Content-Type' , 'application/json');
                xhr.setRequestHeader('X-Cloud-Trace-Context' , 'f2dd29a5a475c5489584b993c3ce670d');
                xhr.setRequestHeader('Date' , 'Thu, 14 Mar 2019 09:43:04 GMT');
                xhr.setRequestHeader('Server' , 'Google Frontend');
                xhr.setRequestHeader('Content-Length' , '100');
            },
            success: function (result) {
                var f = result;
                var content = '';
                var i;
                for (i = 0; i <= f[i] ; i++) {
                    content += "<tr>";
                    content = content+"<td>"+f[i].title+"</td>";
                    content = content + "</tr>";
                }
                $("#tbody").append(content);
                $this.html($this.data('original-text'));
                $("#search").attr("disabled", false);
            }});      
      });
   </script>
</body>

我尝试了最后4个小时,但没有解决方法... 预先感谢您的帮助...

2 个答案:

答案 0 :(得分:2)

我不认为这是绝对的答案,因为我在构建的Chrome扩展程序中也遇到了相同的错误。现在,根据CORB(跨源读取阻止)的建议,Chrome团队更新了73+版浏览器的安全性,以防止出现幽灵和崩溃漏洞。

在此链接上,他们提供了开发人员应如何解决此更新中的错误的方法:https://www.chromium.org/Home/chromium-security/corb-for-developers

其父资源是:https://www.chromestatus.com/feature/5629709824032768,当我找到修复程序时,将使用它来更新此帖子。

答案 1 :(得分:1)

我最近在Chrome扩展程序中也遇到了这个问题,并且按照3. Limit Cross-Origin Requests in Background Pages的Chrome小组的建议,通过将所有HTTP请求移至后台页面来关闭它

  

如果扩展程序的后台页面仅获取并中继内容脚本选择的任何URL(有效地充当开放代理),那么就会发生类似的安全问题。也就是说,受感染的渲染器进程可能会劫持内容脚本,并要求后台页面获取并中继攻击者选择的敏感URL。相反,后台页面应该仅从扩展作者想要的URL中获取数据,理想情况下,URL是一小部分URL,不会使用户的敏感数据受到威胁。