• <tfoot id='rhDtN'></tfoot>

      <small id='rhDtN'></small><noframes id='rhDtN'>

    1. <i id='rhDtN'><tr id='rhDtN'><dt id='rhDtN'><q id='rhDtN'><span id='rhDtN'><b id='rhDtN'><form id='rhDtN'><ins id='rhDtN'></ins><ul id='rhDtN'></ul><sub id='rhDtN'></sub></form><legend id='rhDtN'></legend><bdo id='rhDtN'><pre id='rhDtN'><center id='rhDtN'></center></pre></bdo></b><th id='rhDtN'></th></span></q></dt></tr></i><div id='rhDtN'><tfoot id='rhDtN'></tfoot><dl id='rhDtN'><fieldset id='rhDtN'></fieldset></dl></div>
        <bdo id='rhDtN'></bdo><ul id='rhDtN'></ul>
      <legend id='rhDtN'><style id='rhDtN'><dir id='rhDtN'><q id='rhDtN'></q></dir></style></legend>

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

        时间:2023-09-03
      1. <legend id='31sRq'><style id='31sRq'><dir id='31sRq'><q id='31sRq'></q></dir></style></legend>
        <i id='31sRq'><tr id='31sRq'><dt id='31sRq'><q id='31sRq'><span id='31sRq'><b id='31sRq'><form id='31sRq'><ins id='31sRq'></ins><ul id='31sRq'></ul><sub id='31sRq'></sub></form><legend id='31sRq'></legend><bdo id='31sRq'><pre id='31sRq'><center id='31sRq'></center></pre></bdo></b><th id='31sRq'></th></span></q></dt></tr></i><div id='31sRq'><tfoot id='31sRq'></tfoot><dl id='31sRq'><fieldset id='31sRq'></fieldset></dl></div>

            <tbody id='31sRq'></tbody>
          <tfoot id='31sRq'></tfoot>

                <small id='31sRq'></small><noframes id='31sRq'>

                  <bdo id='31sRq'></bdo><ul id='31sRq'></ul>
                  本文介绍了处理响应 - 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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:CORS 错误:“请求仅支持协议方案:http……"等 下一篇:将模式设置为“no-cors"时使用 fetch 访问 API 时

                  相关文章

                  最新文章

                • <tfoot id='K7RDe'></tfoot>

                    <bdo id='K7RDe'></bdo><ul id='K7RDe'></ul>

                    <small id='K7RDe'></small><noframes id='K7RDe'>

                  1. <i id='K7RDe'><tr id='K7RDe'><dt id='K7RDe'><q id='K7RDe'><span id='K7RDe'><b id='K7RDe'><form id='K7RDe'><ins id='K7RDe'></ins><ul id='K7RDe'></ul><sub id='K7RDe'></sub></form><legend id='K7RDe'></legend><bdo id='K7RDe'><pre id='K7RDe'><center id='K7RDe'></center></pre></bdo></b><th id='K7RDe'></th></span></q></dt></tr></i><div id='K7RDe'><tfoot id='K7RDe'></tfoot><dl id='K7RDe'><fieldset id='K7RDe'></fieldset></dl></div>
                    <legend id='K7RDe'><style id='K7RDe'><dir id='K7RDe'><q id='K7RDe'></q></dir></style></legend>