<i id='BqDNV'><tr id='BqDNV'><dt id='BqDNV'><q id='BqDNV'><span id='BqDNV'><b id='BqDNV'><form id='BqDNV'><ins id='BqDNV'></ins><ul id='BqDNV'></ul><sub id='BqDNV'></sub></form><legend id='BqDNV'></legend><bdo id='BqDNV'><pre id='BqDNV'><center id='BqDNV'></center></pre></bdo></b><th id='BqDNV'></th></span></q></dt></tr></i><div id='BqDNV'><tfoot id='BqDNV'></tfoot><dl id='BqDNV'><fieldset id='BqDNV'></fieldset></dl></div>
    <legend id='BqDNV'><style id='BqDNV'><dir id='BqDNV'><q id='BqDNV'></q></dir></style></legend>
  • <small id='BqDNV'></small><noframes id='BqDNV'>

      <tfoot id='BqDNV'></tfoot>
          <bdo id='BqDNV'></bdo><ul id='BqDNV'></ul>

        fetch() 输入意外结束

        时间:2023-09-03

        • <legend id='nLPrL'><style id='nLPrL'><dir id='nLPrL'><q id='nLPrL'></q></dir></style></legend>

              <tbody id='nLPrL'></tbody>

          1. <tfoot id='nLPrL'></tfoot>
              <bdo id='nLPrL'></bdo><ul id='nLPrL'></ul>

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

                1. <i id='nLPrL'><tr id='nLPrL'><dt id='nLPrL'><q id='nLPrL'><span id='nLPrL'><b id='nLPrL'><form id='nLPrL'><ins id='nLPrL'></ins><ul id='nLPrL'></ul><sub id='nLPrL'></sub></form><legend id='nLPrL'></legend><bdo id='nLPrL'><pre id='nLPrL'><center id='nLPrL'></center></pre></bdo></b><th id='nLPrL'></th></span></q></dt></tr></i><div id='nLPrL'><tfoot id='nLPrL'></tfoot><dl id='nLPrL'><fieldset id='nLPrL'></fieldset></dl></div>
                  本文介绍了fetch() 输入意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 fetch() 从 api 服务器获取数据.我的错误如下所示:

                  I am using fetch() to grab data from api server. My error looks like this:

                  Uncaught (in promise) SyntaxError: Unexpected end of input at 
                    fetch.then.blob.
                  

                  你能告诉我我做错了什么吗?

                  Can you please tell me what am I doing wrong.

                  const weatherAPi ='https://www.metaweather.com/api/location/523920';
                  fetch(weatherAPi, {
                    mode: 'no-cors'
                  }).then(blob => blob.json())
                    .then(data => console.log(data))
                  

                  推荐答案

                  不透明的响应

                  对跨域资源的 no-cors 请求的响应具有 不透明"的响应类型.如果您在尝试将其转换为 JSON 之前记录响应,您将看到一种不透明"类型.

                  Opaque Responses

                  A response for a no-cors request to a cross-origin resource has a response type of 'opaque'. If you log the response before trying to turn it to JSON, you will see a type of "opaque".

                  不透明类型被列为严格限制"whatwg.org 上的 fetch 规范中解释了.

                  Opaque types are listed as "severely restricted" as explained in the fetch spec on whatwg.org.

                  不透明过滤响应是一个过滤响应,其类型为不透明",url列表为空列表,状态为0,状态消息为空字节序列,头部列表为空,正文为空,尾部为空.

                  An opaque filtered response is a filtered response whose type is "opaque", url list is the empty list, status is 0, status message is the empty byte sequence, header list is empty, body is null, and trailer is empty.

                  当类型不透明时,当前无法读取它们,如 Google 关于 opaque 类型的文档.

                  They cannot currently be read when the type is opaque as explained on Google's docs on the opaque type.

                  不透明响应是针对不返回 CORS 标头的不同来源的资源发出的请求.对于不透明的响应,我们将无法读取返回的数据或查看请求的状态,这意味着我们无法检查请求是否成功.使用当前的 fetch() 实现,不可能从窗口全局范围请求不同来源的资源.

                  An opaque response is for a request made for a resource on a different origin that doesn't return CORS headers. With an opaque response, we won't be able to read the data returned or view the status of the request, meaning we can't check if the request was successful or not. With the current fetch() implementation, it's not possible to make requests for resources of a different origin from the window global scope.

                  在您的服务器上启用 CORS 支持

                  这可以取决于环境或语言.例如,您可以通过更改服务器配置来更改 Nginx 环境中的 CORS 设置,或者您可以在应用程序代码(例如 PHP)中指定标头.

                  Enable CORS support on your server

                  This can be environment-dependent or language-dependent. For example, you can change CORS settings within Nginx's environment by changing your server config, or you can specify headers within your application code such as in PHP.

                  我强烈建议阅读 Mozilla 关于 CORS 请求的文档 以及 Access-Control-Allow-起源.

                  I highly recommend reading the Mozilla documentation on CORS requests and also Access-Control-Allow-Origin.

                  PHP 中的一个例子:

                  An example in PHP:

                  <?php
                  header("Access-Control-Allow-Origin: *");  // "*" could also be a site such as http://www.example.com
                  

                  这篇关于fetch() 输入意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:跨域资源共享 GET:'拒绝获取不安全的标头“ 下一篇:Bottle Py:为 jQuery AJAX 请求启用 CORS

                  相关文章

                  最新文章

                  <legend id='u1ZK6'><style id='u1ZK6'><dir id='u1ZK6'><q id='u1ZK6'></q></dir></style></legend>

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

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

                    1. <tfoot id='u1ZK6'></tfoot>
                        <bdo id='u1ZK6'></bdo><ul id='u1ZK6'></ul>