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

Handle response - SyntaxError: Unexpected end of input when using mode: #39;no-cors#39;(处理响应 - SyntaxError:使用模式时输入意外结束:no-cors)
本文介绍了处理响应 - SyntaxError:使用模式时输入意外结束:'no-cors'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了对 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)

这篇关于处理响应 - SyntaxError:使用模式时输入意外结束:'no-cors'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)