
我有一個帶有 HTML 表單的網站,提交後會成功將 POST 請求發送到伺服器上的 .php 檔案(Apache 2.4.48)。
但是,當我讓 Javascript 透過 JS fetch() 處理提交時,伺服器回應 405 錯誤。
因此,我分析了兩個 POST 的請求和回應標頭,它們幾乎相同,所以我對為什麼第一個方法有效而另一個方法被拒絕感到困惑。
以下是透過 HTML 表單傳送時的請求/回應(感嘆號是 POST 請求不同的地方):
GENERAL
Request URL: https://example.com/php/script.php
Request Method: POST
Status Code: 302
Remote Address: 160.153.133.187:443
Referrer Policy: origin-when-cross-origin
RESPONSE HEADERS
cache-control: max-age=0 !
content-length: 0
content-type: text/html; charset=UTF-8
date: Sat, 14 Aug 2021 23:21:34 GMT
expires: Sat, 14 Aug 2021 23:21:34 GMT
location: https://example.com/pages/form-submitted.html#submitted
server: Apache
vary: User-Agent
x-powered-by: PHP/8.0.8
REQUEST HEADERS
:authority: example.com
:method: POST
:path: /php/script.php
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 !
accept-encoding: gzip, deflate, br
accept-language: en-GB,en;q=0.9,es-ES;q=0.8,es;q=0.7,it-IT;q=0.6,it;q=0.5
cache-control: no-cache
content-length: 96
content-type: application/x-www-form-urlencoded !
dnt: 1
origin: https://example.com
pragma: no-cache
referer: https://example.com/contact
sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
sec-ch-ua-mobile: ?0
sec-fetch-dest: document !
sec-fetch-mode: navigate !
sec-fetch-site: same-origin
sec-fetch-user: ?1
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
FORM DATA
name: Tai Zen
email: [email protected]
phone-number: 12345678910
privacy-consent: on
submit:
以下是使用 fetch() 發送 POST 時的請求/回應:
GENERAL
Request URL: https://example.com/php/script.php
Request Method: POST
Status Code: 302
Remote Address: 160.153.133.187:443
Referrer Policy: same-origin
RESPONSE HEADERS
cache-control: max-age=2741
content-length: 0
content-type: text/html; charset=UTF-8
date: Sat, 14 Aug 2021 23:31:44 GMT
expires: Sun, 15 Aug 2021 00:17:26 GMT
location: https://example.com/405
server: Apache
vary: User-Agent
x-powered-by: PHP/8.0.8
REQUEST HEADERS
:authority: example.com
:method: POST
:path: /php/script.php
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-GB,en;q=0.9,es-ES;q=0.8,es;q=0.7,it-IT;q=0.6,it;q=0.5
cache-control: no-cache
content-length: 471
content-type: multipart/form-data; boundary=----WebKitFormBoundary28YayN0mmqwdpQh0
dnt: 1
origin: https://example.com
pragma: no-cache
referer: https://example.com/contact
sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
sec-ch-ua-mobile: ?0
sec-fetch-dest: empty
sec-fetch-mode: same-origin
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
FORM DATA
name: Tai Zen
email: [email protected]
phone-number: 12345678910
privacy-consent: on
submit:
我應該更改伺服器端的某些內容以允許對該特定檔案發出 POST 請求嗎?
但是,我希望它只接收來自我寫的 JS 而不是外部實體的 POST 請求,但我不知道該怎麼做。我嘗試了建議的內容這個答案,但是沒有成功,伺服器給了500錯誤。我想這可能是因為我使用的是共享託管計劃,我無法完全訪問我的 Apache 設置,但我不確定這是原因。