【问题标题】:JavaScript: Cross-Origin Read Blocking (CORB) blocked cross-origin response when request through gitlab apiJavaScript:通过 gitlab api 请求时,跨域读取阻止 (CORB) 阻止了跨域响应
【发布时间】:2019-12-02 16:31:32
【问题描述】:

我已经通过gitlab api成功接收到用户的id,但是当我尝试接收用户的项目列表时,chrome返回CORB。

我已经使用 GitLab api v4 成功收到了 id 和所有其他用户信息。但是当我试图接收用户的 repo 列表时,我收到了 CORB 块并且在响应文本中一无所获。直接输入网址可以看到json格式的数据。如果我直接使用 jQuery.getJSON,它会要求我在 header 中添加 X-Requested-With 或 Origin,我不知道如何实现。

                        var target = 'https://gitlab.com/api/v4/users/'+doc.data().gitlab_id+'/projects?callback=?';
                        console.log(target);
                        var xhr = new XMLHttpRequest();
                        xhr.onreadystatechange = process;
                        xhr.open("GET", target, true);
                        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                        xhr.setRequestHeader('responseType', 'text/plain');
                        xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
                        xhr.send();
                        function process() {
                            console.log(xhr);

我希望得到 json 数据,但现在响应文本为空,并返回以下消息:

跨域读取阻塞 (CORB) 阻止了 MIME 类型为 application/json 的跨域响应 https://gitlab.com/api/v4/users/USER_ID/projects?callback=jQuery11130057655514217979986_1563975117007&_=1563975117008。详情请见https://www.chromestatus.com/feature/5629709824032768

【问题讨论】:

    标签: google-chrome http get xmlhttprequest gitlab-api


    【解决方案1】:

    对于遇到同样问题的人:

    尝试在您的请求网址前添加“https://cors-anywhere.herokuapp.com/”。

    例如,您可以使用 'https://cors-anywhere.herokuapp.com/https://api.linkedin.com/v2/me?oauth2_access_token=' + access_token; 从linkedin获取信息而不受cors政策的困扰。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-22
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      相关资源
      最近更新 更多