簡體   English   中英

Javascript XHR 向 php 文件發送請求:加載資源失敗:服務器響應狀態為 404(未找到)

[英]Javascript XHR Post Request to php file: Failed to load resource: the server responded with a status of 404 (Not Found)

我正在嘗試將 formData 發布到 php 文件中。 我不斷收到以下錯誤”

加載資源失敗:服務器響應狀態為 404(未找到)

index.html 文件和 test.php 文件位於同一目錄下。

我嘗試將文件移動到不同的文件夾。 我試過了。/../ http://localhost/...等

javascript

    const ctForm = document.getElementById('ctForm');

    ctForm.addEventListener('submit', (e)=>{
        e.preventDefault();

        const data = new FormData(ctForm);

        const xhr = new XMLHttpRequest();

        xhr.open("POST", "test.php", true);

        xhr.send(data);
    })

html

    <form id="ctForm" class="contact-form">
        <label>Your info:</label>
        <div class="client-info">
            <input type="text" name="firstname" placeholder="first name" required>
            <input type="text" name="lastname" placeholder="last name" required>
            <input type="e-mail" name="email" placeholder="e-mail" required>
            <input type="phone" name="phone" placeholder="phone no" required>
            <input type="hidden" name="subject" value="Estimate Request">
            <textarea class="message" name="message" placeholder="Message"></textarea> 
        <button class="send" type="submit" value="Send">Send</button>
        </div>
    </form>

測試.php

    var_dump($_POST);

如果您能提供幫助,請提前致謝

假設您使用的是本地主機,您的 URL 字符串應該是

http://localhost/{project_folder_name}/test.php而不是只有test.php ,如下所示。

xhr.open("POST", "http://localhost/{project_folder_name}/test.php");

您可以忽略第三個參數,如mdn doc中所述,第三個參數默認為true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM