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

    • <bdo id='U1Tc8'></bdo><ul id='U1Tc8'></ul>
    <tfoot id='U1Tc8'></tfoot>

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

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

        发送自定义用户代理字符串以及我的标头(获取)

        Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
          <tbody id='7Nxnf'></tbody>
          <bdo id='7Nxnf'></bdo><ul id='7Nxnf'></ul>

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

              <tfoot id='7Nxnf'></tfoot>
              • <small id='7Nxnf'></small><noframes id='7Nxnf'>

                  本文介绍了发送自定义用户代理字符串以及我的标头(获取)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 React 中使用 fetch API,并从 JSON 端点提取一些数据.

                  I'm using the fetch API in React, and I'm pulling down some data from a JSON endpoint.

                  作为我请求的一部分,我想发送一个自定义 User-Agent 字符串.目前,当我检查我的请求时,UA 字符串是:

                  As part of my requests, I want to send a custom User-Agent string. Currently, when I inspect my requests, the UA string is:

                  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
                  

                  因为我在每个请求中都输入了标头,所以我想我只需将 User-Agent 附加到标头对象,就像它在 各种 地点在线:

                  Since I'm pasing in headers with every request, I figured I'd just append User-Agent to the headers object, like it says in various places online:

                  fetch(url, {
                      Accept: 'application/json',
                      'Content-Type': 'application/json',
                      'User-Agent': 'MY-UA-STRING' // <---
                  })
                  

                  但这不起作用.正如 here 报道的那样,我感觉这是因为 fetch api 中的错误 和 这里 和 这里.

                  But this doesn't work. I have a feeling it's becuase of a bug in the fetch api, as reported here and here and here.

                  任何人都可以解决如何使用 fetch 将 UA 作为 headers 的一部分传递?

                  Anyone have a work around on how to pass UA as part of the headers using fetch?

                  推荐答案

                  经过一些测试,Chrome确实存在User-Agent header的bug.

                  After some testing, Chrome does indeed have a bug with the User-Agent header.

                  这很可能是因为不久前(2015 年年中)User-Agent 标头在不允许的标头列表中.

                  This is most likely due to the fact that the User-Agent header was on the list of disallowed headers not too long ago (mid 2015).

                  由于此特定标头最近已从不允许标头列表中删除,Firefox(从版本 43 开始)将允许您在 fetch 调用中更改 User-Agent,但 Chrome 不会.

                  As this particular header was recently removed from the list of disallowed headers, Firefox (from version 43) will let you change the User-Agent in a fetch call, but Chrome won't.

                  这是 Firefox 错误和 Chromium 错误

                  首先不允许它的原因是,确实没有充分的理由使用 User-Agent 标头发送任意数据,它应该用于发送实际的 User-Agent 和浏览器内请求,例如无论如何,XMLHttpRequest 的获取确实没有充分的理由来欺骗用户代理.

                  The reason it was disallowed in the first place, was that there's really no good reason to use the User-Agent header to send arbitrary data, it should be used to send the actual User-Agent, and in-browser requests like Fetch of XMLHttpRequest should really have no good reason to spoof the User Agent anyway.

                  这个 bug 什么时候会在 Chrome 中修复是任何人的猜测,但这确实是一个 bug,因为不允许的头列表不再列出 User-Agent 头,当在 Fetch 的选项对象中指定时它应该会改变.

                  When the bug will be fixed in Chrome is anyones guess, but it is indeed a bug as the list of disallowed headers no longer lists the User-Agent header, and it should change when specified in the options object of Fetch.

                  作为旁注,您通常应该使用 Headers 创建标题接口,并将它们包含在 options 对象中 在 headers 键下

                  As a sidenote, you should generally be creating the headers using the Headers Interface, and include them in the options objects under the headers key

                  let headers = new Headers({
                      "Accept"       : "application/json",
                      "Content-Type" : "application/json",
                      "User-Agent"   : "MY-UA-STRING"
                  });
                  
                  fetch(url, {
                      method  : 'GET', 
                      headers : headers 
                      // ... etc
                  }).then( ...
                  

                  这篇关于发送自定义用户代理字符串以及我的标头(获取)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  can#39;t get response status code with JavaScript fetch(无法使用 JavaScript 获取响应状态代码)

                      <tfoot id='wgZD9'></tfoot>
                        • <small id='wgZD9'></small><noframes id='wgZD9'>

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

                              <tbody id='wgZD9'></tbody>