javascript - 使用 fetch 创建 get 请求 - 只有选项被发送

标签 javascript ecmascript-6 request http-get fetch-api

我正在尝试使用 Fetch 创建获取请求。请求看起来像这样:

fetch('https://requestb.in/14ikb6j1', {
  method: 'get',
  headers: {
    'Authorization': 'Token token=xxxxx'
  }});

如果我向自己的服务器发出此请求,它每次都会在选项请求上失败。我不明白为什么。

如果我向 requestbin 发出这个请求,首先会有一个选项请求。然而,在收到选项响应后,实际的获取请求从未发出。

这是 requestbin 收到的内容:

OPTIONS /14ikb6j1

HEADERS

Referer: http://localhost:9000/
Host: requestb.in
Total-Route-Time: 0
Via: 1.1 vegur
Accept-Encoding: gzip
Cf-Visitor: {"scheme":"https"}
Cf-Ipcountry: NL
Cf-Ray: 36edd35d1cad2b28-AMS
Cf-Connecting-Ip: 37.153.231.90
Connection: close
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Accept: */*
Connect-Time: 1
Access-Control-Request-Method: GET
Content-Length: 0
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
X-Request-Id: 69b120c1-0f18-454b-a7fd-91f082252a06
Access-Control-Request-Headers: authorization
Origin: http://localhost:9000

这是 chrome 记录的内容:

chrome dev tools


最奇怪的是,结果我得到了 200 - OK 状态。然而,控制台仍然记录这个:

Fetch API cannot load https://requestb.in/14ikb6j1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. :9000/#/branching:1 Uncaught (in promise) TypeError: Failed to fetch

但如果我使用 Postman 发出此请求,它确实有效:postman request

GET /14ikb6j1
HEADERS

Cf-Ipcountry: NL
Cf-Ray: 36eddb797ebf7223-AMS
Authorization: Token token=xxxx
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Total-Route-Time: 0
Via: 1.1 vegur
Connection: close
Cf-Connecting-Ip: 37.153.231.90
Connect-Time: 0
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Postman-Token: 57486701-6f9a-4aab-be1b-776f0d376920
Accept: */*
Host: requestb.in
X-Request-Id: 73a7a35e-0d89-41fd-b760-2aa952349871
Accept-Encoding: gzip
Cf-Visitor: {"scheme":"https"}
Cache-Control: no-cache

如何使带有身份验证 header 的 get 请求生效?

最佳答案

这可能是一个 CORS 问题。您发送请求的服务器不允许来自不同域的请求。 检查您的浏览器控制台并查找错误。你可能会发现这样的东西:

Fetch API cannot load https://requestb.in/14ikb6j1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:9000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

关于javascript - 使用 fetch 创建 get 请求 - 只有选项被发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44546445/

相关文章:

javascript - 第二个 Javascript 表单 POST 在 Chrome 中失败,在 IE 和 FF 中有效

c# - 如何根据 asp.net 中的分辨率以编程方式创建填充客户端浏览器的按钮列表

javascript - 访问控制允许来源错误

javascript - 解释箭头运算符系列

request - 如何在中间件和处理程序中读取 Iron Request?

php - 意外的标识符 预期结束 ']' 或数组元素后的 ','

javascript - 使用 Javascript 设置高度很慢

javascript - 为什么 Angular 使用 `import { SomeComponent } from ' some.component.ts '` instead of ` import SomeComponent from 'some.component.ts' `

JavaScript - 用 async/await 替换 setTimeout

node.js - 抓取 Nodejs