実現したいこと
Apacheでオリジン間リソース共有 (CORS)を利用したときに、プリフライト・リクエストが発生するPCと、発生しないPCがあり困っています。
(試したこと)
①apacheのhttpd.confとaxiosのヘッダにより、リクエストが、単純リクエストになるようにし
自分のPCで社内用Webシステムからaxiosでlocalhostにアクセスしたところ、プリフライト・リクエストが発生しなかった。
Google Chromeコンソールに警告なし、何も表示なし。
②他のPCも同じapacheのhttpd.confの設定をして、
同じWebシステムからaxiosでlocalhostにアクセスしたところ、プリフライト・リクエスト?(2回送信)が発生した。
Google Chromeコンソールに警告なし、何も表示なし。
設定での対処方法がなければ、PHPでプリフライトリクエストを判定する方法があればよいのですが。
わかるかたいらっしゃいましたらお願いします。
現在のapacheの設定と、axiosでリクエストするコード
apacheの設定
httpd.conf
1<Directory "C:/xampp/htdocs"> 2 # ... 3 4 Header set Access-Control-Allow-Origin "http://192.168.11.30:3000" 5 Header set Access-Control-Allow-Methods "GET" 6 Header set Access-Control-Allow-Headers "Origin, withcredentials" 7 Header set Access-Control-Allow-Credentials "true" 8</Directory>
axiosでリクエストするコード(localhostに対してgetアクセスしています。localhostはapacheの80ポートを利用)
index.js
1axios({ 2 method: "get", 3 url: "http://localhost", 4 headers: { 5 "Content-Type": "application/x-www-form-urlencoded;" 6 }, 7 params: { value: "GETで渡すパラメーター" } 8}).then((response) => { 9 console.log("ok") 10}).catch((response) => { 11 console.log("ng") 12});
パケットキャプチャソフトによる、プリフライト・リクエストと、2回目のリクエストはこちらです。
// プリフライト・リクエスト(OPTIONS) OPTIONS *** Host: localhost Connection: keep-alive Accept: */* Access-Control-Request-Method: GET Access-Control-Request-Headers: withcredentials User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Origin: http://192.168.11.30:3000 Sec-Fetch-Site: cross-site Sec-Fetch-Mode: cors Sec-Fetch-Dest: empty Accept-Encoding: gzip, deflate, br Accept-Language: ja,en-US;q=0.9,en;q=0.8 // 2回目のリクエスト GET *** Host: localhost Connection: keep-alive sec-ch-ua: "Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24" Accept: application/json, text/plain, */* withCredentials: true sec-ch-ua-mobile: ?0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 sec-ch-ua-platform: "Windows" Origin: http://192.168.11.30:3000 Sec-Fetch-Site: cross-site Sec-Fetch-Mode: cors Sec-Fetch-Dest: empty Accept-Encoding: gzip, deflate, br Accept-Language: ja,en-US;q=0.9,en;q=0.8

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/28 01:42 編集
2022/10/28 01:52
2022/10/28 01:56 編集
2022/10/28 01:54