为什么这个ajax调用不起作用?

时间:2014-11-12 00:10:57

标签: javascript php jquery ajax

$(document).ready(function(){

    $('.clickthetext').click(function(){

        $.post("submit.php", $("#formbox").serialize(),  function(response) {
            $('#content').html(response);
        });
        return false;
    });

});

我的目标是从表单传递内容并编辑数据并在当前页面显示响应。

.clickthetext 按钮内容:

<div class="clickthetext">Click here to see the result</div>

内容ID #formbox
此id中的部分表单。表格的其余部分在外面,此ID将在稍后处理。只有id&#34; formbox&#34;内的内容/输入将被处理。

无论我们将得到什么样的回应,我们都会在内容中展示#34;#content&#34;标识。

我在这里做错了什么?

---- ----编辑

我没有在submit.php上添加任何内容 只是为了表示回应,我在那里写道:

<?php
 echo 'somthing blah blah blah something';
?>

2 个答案:

答案 0 :(得分:1)

可能submit.php

的结果存在问题

您可以尝试拨打

$(document).ready(function(){

    $('.clickthetext').click(function(){

        $.ajax({
          type: "POST",
          url: "submit.php",
          data: $("#formbox").serialize(),
          success: function(response) {  $('#content').html(response); },
          error: function(jqXHR, textStatus, errorThrown) { console.log(textStatus, errorThrown); },
          dataType: dataType
});
        return false;
    });

});

而是获取ajax调用结果的更多细节。

Here's the API for the ajax object in jQuery

答案 1 :(得分:0)

重新创建您的设置,

<强> JS / HTML

<form action="" id="formbox">
    <input type="text" name="firstName" value="First Name">
</form>
<button class="clickthetext">Button</button>

<div id="content"></div>

<script>
    jQuery(document).ready(function ($) {

        $('.clickthetext').click(function() {
            $.post("submit.php", $("#formbox").serialize(), function (response) {
                $('#content').html(response);
            })
        })
    });
</script>

PHP: submit.php

<?php echo 'this is the response'; ?>

一切都很完美。

调试提示:

1)最有可能 - 检查您的JavaScript控制台是否有任何错误。您可能在页面的其他位置有错误。

2)确保您通过localhost而不是文件路径使用javascript访问HTML页面

3)不太可能,但请检查您的PHP日志。