简体   繁体   English

使用自定义标头ReactJS获取GET请求

[英]Fetch GET Request with custom headers ReactJS

I am trying to send a GET request to a API but when i add custom headers in the code somthing strange happens. 我试图将GET请求发送到API,但是当我在代码中添加自定义标头时,发生了奇怪的事情。 Somewhere the request method changes to OPTIONS when it reaches the web server. 当请求方法到达Web服务器时,它在某处变为OPTIONS。

But when i do the same without headers it will be a GET type. 但是,当我不使用标题执行相同操作时,它将是GET类型。 When i use the application postman (API development tool) the request works fine! 当我使用应用程序邮递员(API开发工具)时,请求工作正常!

request code: 请求代码:

  let token = this.generateClientToken(privateKey, message); let myheaders = { "appID": appID, "authorizationkey": token } fetch('http://localhost:8080/api/app/postman', { method: "GET", // body: JSON.stringify(''), headers: myheaders }).then(function(response) { console.log(response.status); //=> number 100–599 console.log(response.statusText); //=> String console.log(response.headers); //=> Headers console.log(response.url); //=> String return response.text() }, function(error) { console.log(error.message); //=> String }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

Server log ouput (with headers): 服务器日志输出(带标题):

worker_1  | 172.18.0.4 -  17/Mar/2017:15:47:44 +0000 "OPTIONS /index.php" 403
web_1     | 172.18.0.1 - - [17/Mar/2017:15:47:44 +0000] "OPTIONS /api/app/postman HTTP/1.1" 403 5 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:54.0) Gecko/20100101 Firefox/54.0" "-"

Server log output (without headers): 服务器日志输出(不包含标题):

worker_1  | 172.18.0.4 -  17/Mar/2017:16:01:49 +0000 "GET /index.php" 403
web_1     | 172.18.0.1 - - [17/Mar/2017:16:01:49 +0000] "GET /api/app/postman HTTP/1.1" 403 5 "http://localhost:3000/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:54.0) Gecko/20100101 Firefox/54.0" "-"

Added the NPM modules for fetch support in extra browsers: 添加了NPM模块以在其他浏览器中获取支持:
https://github.com/github/fetch#obtaining-the-response-url https://github.com/github/fetch#obtaining-the-response-url
https://github.com/taylorhakes/promise-polyfill https://github.com/taylorhakes/promise-polyfill

What am i missing here? 我在这里想念什么? It all looks correct to me. 在我看来,这一切都是正确的。

I am using firefox development edition to test the Reactjs app by running it with NPM start 我正在使用firefox开发版通过NPM start运行它来测试Reactjs应用

The accepted answer game me the solution, i am not using a nodeJS backend but plain Nginx with php-fpm. 可接受的答案是我的解决方案,我不是使用nodeJS后端,而是使用带有php-fpm的纯Nginx。

But the answer explains how a request with custom header wil always first do a OPTIONS request to verify the acceptance of the set header names, so i had to change the response in the webserver to give a 204 code back with te right headers included. 但是答案解释了如何使用自定义标头的请求总是首先执行OPTIONS请求以验证是否接受设置的标头名称,因此我不得不在网络服务器中更改响应,以返回包含正确标头的204代码。 without it would hit my PHP code where authentication would fail and result in a 403 code because of the absence of the headers with the content and request method used. 没有它会打到我的PHP代码,在此代码中身份验证将失败并导致403代码,因为缺少带有所使用内容和请求方法的标头。

This is what i added to the Nginx host to make it work: 这是我添加到Nginx主机以使其工作的内容:

location ~ \.php$ {
             add_header 'Access-Control-Allow-Origin' "*";
             add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
             add_header 'Access-Control-Allow-Headers' 'appID,authorizationkey';

             if ($request_method = 'OPTIONS') {
                return 204;
             }
}

I know that its far from perfect but for now it made it work. 我知道它远非完美,但就目前而言,它确实可行。 and again thanks for pointing me into the right direction. 再次感谢您为我指明了正确的方向。

You probably want to install the cors npm package https://www.npmjs.com/package/cors on the server where you have your http://localhost:8080/api/app Node app running. 您可能要在运行http://localhost:8080/api/app Node应用程序的服务器上安装cors npm软件包https://www.npmjs.com/package/cors

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests has details about what's happening here: Your appID and authorizationkey request headers are triggering your browser to send a CORS preflight OPTIONS request before sending the GET . https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS#Preflighted_requests包含有关此处发生情况的详细信息:您的appIDauthorizationkey请求标头触发您的浏览器在发送CORS预检OPTIONS请求之前GET

To handle that OPTIONS request, you can install the cors npm package and follow the instructions at https://www.npmjs.com/package/cors#enabling-cors-pre-flight to configure it: 要处理该OPTIONS请求,您可以安装cors npm软件包并按照https://www.npmjs.com/package/cors#enabling-cors-pre-flight上的说明进行配置:

var express = require('express')
  , cors = require('cors')
  , app = express();
app.options('*', cors()); // include before other routes
app.listen(80, function(){
  console.log('CORS-enabled web server listening on port 80');
});

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

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