处理响应 - SyntaxError:使用模式时输入意外结束:'no-cors'

2022-01-15 00:00:00 json cors reactjs javascript fetch-api

我尝试了对 REST-API 的 ReactJS fetch 调用并希望处理响应.通话有效,我得到了响应,我可以在 Chrome 开发工具中看到:

I tried a ReactJS fetch call to a REST-API and want to handle the response. The call works, i get a response, which i can see in Chrome Dev Tools:

function getAllCourses() {
fetch('http://localhost:8080/course', {
    method: 'POST',
    mode: 'no-cors',
    credentials: 'same-origin',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        objectClass: 'course',
        crud: '2'
    })
}).then(function (response) {
    console.log(response);
    return response.json();

}).catch(function (err) {
    console.log(err)
});
}

当我尝试处理响应时,我在

When i try to handle the response, i got a "SyntaxError: Unexpected end of input" at

return response.json();

console.log 如下所示:

The console.log looks like this:

我的响应 JSON 看起来是这样的,它是有效的,我用 jsonlint 检查了它:

My Response JSON looks like this, it is valid, i checked it with jsonlint:

[
  {
    "0x1": {
      "users": [],
      "lectures": [],
      "owner": "0x2",
      "title": "WWI 14 SEA",
      "description": null,
      "objectClass": "course",
      "id": "course_00001"
    },
    "0x2": {
      "username": "system",
      "lectures": [],
      "course": null,
      "solutions": [],
      "exercises": [],
      "roles": [
        "0x3",
        "0x4",
        "0x5"
      ],
      "objectClass": "user",
      "id": "user_00001"
    },
    "0x3": {
      "roleName": "ROLE_ADMIN",
      "objectClass": "role",
      "id": "role_00001"
    },
    "0x4": {
      "roleName": "ROLE_STUDENT",
      "objectClass": "role",
      "id": "role_00002"
    },
    "0x5": {
      "roleName": "ROLE_DOCENT",
      "objectClass": "role",
      "id": "role_00003"
    }
  }
]

推荐答案

您需要从您的请求中删除 mode: 'no-cors' 设置.设置 no-cors 模式正是您遇到问题的原因.

You need to remove the mode: 'no-cors' setting from your request. Setting no-cors mode is exactly the cause of the problem you’re having.

no-cors 请求使响应类型为 opaque.问题中的日志片段显示了这一点.不透明意味着您的前端 JavaScript 代码看不到响应正文或标头.

A no-cors request makes the response type opaque. The log snippet in the question shows that. Opaque means your frontend JavaScript code can’t see the response body or headers.

https://developer.mozilla.org/en-US/docs/Web/API/Request/mode 说明:

no-cors — JavaScript 可能无法访问结果 Response

no-cors — JavaScript may not access any properties of the resulting Response

所以设置 no-cors 模式的效果本质上是告诉浏览器,在任何情况下都不要让前端 JavaScript 代码访问响应正文或标头."

So the effect of setting no-cors mode is essentially to tell browsers, "Don’t let frontend JavaScript code access the response body or headers under any circumstances."

我想您正在尝试 no-cors 因为来自 http://localhost:8080/course 的响应不包括 Access-Control-Allow-Origin 响应标头,否则因为您的请求会触发 CORS 预检,因此您的浏览器会进行 OPTIONS 预检.

I imagine you’re trying no-cors because the response from http://localhost:8080/course doesn’t include the Access-Control-Allow-Origin response header or else because your request is one that triggers a CORS preflight, and so your browser does an OPTIONS preflight.

但是使用 no-cors 模式并不能解决这些问题.解决方案是:

But using no-cors mode is not the solution to those problems. The solution is either to:

  • 配置 http://localhost:8080 服务器发送 Access-Control-Allow-Origin 响应头并处理 OPTIONS 请求

  • configure the http://localhost:8080 server to send the Access-Control-Allow-Origin response header and to handle the OPTIONS request

或使用 https://github 中的代码设置 CORS 代理.com/Rob--W/cors-anywhere/ 之类的(请参阅如何使用 CORS 代理解决无访问控制允许来源标头"问题部分在尝试从 REST API 获取数据时,请求的资源上不存在Access-Control-Allow-Origin"标头)

or set up a CORS proxy using code from https://github.com/Rob--W/cors-anywhere/ or such (see the How to use a CORS proxy to get around "No Access-Control-Allow-Origin header" problems section of the answer at No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API)

相关文章