APIにGETリクエストを送信しようとしていますが、コードにカスタムヘッダーを追加すると奇妙なことが起こります。 要求メソッドがWebサーバーに到達すると、OPTIONSに変更されます。カスタムヘッダーを使用してGETリクエストを取得するReactJS
しかし、ヘッダーを付けずに同じことをすると、GETタイプになります。 私はアプリケーションの郵便配達員(API開発ツール)を使用すると、リクエストは正常に機能します。
要求コード:
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>
サーバーログouputを(ヘッダーを含む):
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" "-"
サーバーログ出力(ヘッダなし):
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" "-"
は余分のブラウザでサポートをフェッチするためにNPMモジュールを追加しました:
https://github.com/github/fetch#obtaining-the-response-url
https://github.com/taylorhakes/promise-polyfill
を私はここで何をしないのですか?それはすべて私に見えます。
私はあなたがおそらくあなたはhttp://localhost:8080/api/app
ノードのアプリを実行しているサーバー上cors
NPMパッケージhttps://www.npmjs.com/package/corsをインストールしたい
これをチェックしてください。私はCORSと何か関係があると確信しています。http:// stackoverflow。com/questions/27915191/how-does-the-chrome-browser-decide-send-to-send-options –