jQuery-post()方法获取“未找到”错误

时间:2019-05-29 00:15:31

标签: php jquery html ajax

我想发送电子邮件地址到我的submit.php并通过jquery接收回复。

submit.phpindex.html在网站根目录中,而script.js在'js'文件夹中。

我的index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>
<input id="email" type="email" placeholder="  Email"><br>
<a href=""><button id="submit-btn">send</button></a>
<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous">
</script>
<script src="js/script.js"></script>
</body>
</html>


我的script.js

$(document).ready(()=>{
    $("#submit-btn").click(()=>{
        $.post("../submit.php", { user_email: $("#email").val()})
            .done((data)=>{
                alert(data);
            })
            .fail((xhr, status, error)=>{
                alert(error);
            });
    });
});

我的submit.php

<?php
$email = (isset($_POST['user_email']) ? $_POST['user_email'] : "nothing");
echo $email;

我希望输出为我的电子邮件地址,但实际输出为not found。 我认为我的请求失败了

1 个答案:

答案 0 :(得分:1)

由于您说两个文件都在同一个文件夹中,因此不需要../

$.post("../submit.php", { user_email: $("#email").val()})
$.post("submit.php", { user_email: $("#email").val()})