AJAX按钮没有提交而没有通过

时间:2014-09-25 00:58:25

标签: javascript php jquery ajax

我遇到的问题是我的Ajax代码没有通过..好像函数是空的。 我点击提交,没有任何反应..

我的HTML代码:

<head>

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<body>
<form method="POST" id="contactForm" >
<input type="text" name="email" id="email"></input>
<input type="submit" name="submit"></input>

</form>
<script type="text/javascript">
$('#contactForm').submit(function(e){
    e.preventDefault();
    var email = $('#email').val();
    $.ajax({
        type: 'POST',
        dataType: 'JSON',
        url: 'check.php',
        data: {email: email},
        success: function(data){
            if(data.status == 'success') {
                alert('The e-mail address entered is correct.');
            } else {
                alert('The e-mail address entered is Incorrect.');
            }
        }
    });
});
</script>
</body>
</head>

我的check.php:

  <?php

if (isset($_POST['email'])) {
    $status = 'success'
} else {
    $status = 'failed';
}

echo json_encode(array('status' => $status));

?>

当我点击提交时,它什么都不做.. 我希望弹出错误。

我有什么遗漏的吗?

3 个答案:

答案 0 :(得分:1)

确保您传入ajax呼叫的所有选项都已正确设置。示例:数据类型需要变为dataType,而beforesend需要变为beforeSend。

以下是参考:http://api.jquery.com/jquery.ajax/

答案 1 :(得分:1)

括号错位,您错过了:以及&#34;成功&#34;打回来。请参阅下面的固定代码,

这个案子也在这里的另一个答案中提到..

$('#contactForm').submit(function () {
    var email = $('#email').val();
    $.ajax({
        type: 'GET',
        dataType: 'json',
        url: 'check.php',
        beforeSend: function () {},
        //     v--- missing :
        success: function (data) {
            if (data.status == 'success') {
                alert('The e-mail address entered is correct.');
            } else if (data.status !== 'success') {
                alert('The e-mail address entered is wrong.');
            }
        } // this was misplaced in the line below in your code
    }); // this was misplaced in the line above in your code
});

答案 2 :(得分:0)

尝试使用Firefox或Chrome的开发工具进行调试。

例如,在Chrome的DevTools上:

  • 使用source选项调试Javascript。或者你冷。在.submit()函数中添加一些console.log()以确保事件处理程序正在触发。
  • 使用network tab确保正在进行ajax调用,并检查从服务器获得的响应。
  • 最后但同样重要的是,检查console tab是否有Javascript错误,看看是否有其他脚本在您的脚本执行之前抛出错误。

通过这些技术,您可以确定可能出现的问题并了解如何解决问题。