简体   繁体   English

使用fetch-only选项创建get请求

[英]Creating a get request using fetch - only options is send

I'm trying to create a get request using Fetch. 我正在尝试使用Fetch创建一个get请求。 The request looks like this: 请求如下所示:

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

If I make this request to my own server, it fails on the options request every time. 如果我向我自己的服务器发出此请求,则每次都会在选项请求上失败。 I cannot figure out why. 我无法弄清楚为什么。

If I make this request to requestbin, there is an options request first. 如果我向requestbin发出此请求,则首先会有一个选项请求。 However, after receiving the options response, the actual get request is never made. 但是,在收到选项响应后,永远不会发出实际的get请求。

This is what requestbin received: 这就是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

This is what chrome logs: 这是chrome日志:

chrome dev工具


The strangest thing is, I get a 200 - OK status as result. 最奇怪的是,我得到200 - OK状态。 The console however, still logs this: 然而,控制台仍然记录这个:

Fetch API cannot load https://requestb.in/14ikb6j1 . Fetch API无法加载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. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' http://localhost:9000 ' is therefore not allowed access. 因此不允许Origin'http:// localhost:9000 '访问。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. 如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用CORS的资源。 :9000/#/branching:1 Uncaught (in promise) TypeError: Failed to fetch :9000 /#/ branchching:1 Uncaught(in promise)TypeError:无法获取

But if I make this request using Postman it does work: 但是,如果我使用Postman发出此请求,它确实有效: 邮差要求

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

How can I make this get request with authentication headers work? 如何使用身份验证标头生成此get请求?

This is probably a CORS problem. 这可能是一个CORS问题。 The server you are seinding the request to doesn't allow requests from a different domain. 您正在查询请求的服务器不允许来自其他域的请求。 Check your browser console and look for errors. 检查浏览器控制台并查找错误。 You might find somehting like this: 你可能会发现像这样的东西:

Fetch API cannot load https://requestb.in/14ikb6j1 . Fetch API无法加载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. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' https://localhost:9000 ' is therefore not allowed access. 因此,不允许来源“ https:// localhost:9000 ”访问。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. 如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用CORS的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM