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


Why this ajax call is not working?

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

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

.点击文本按钮内容:

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

ID #formbox
内的内容此 ID 内的一部分表单。 表单的其余部分在外面,此 ID 将在稍后处理。仅处理 ID"表单框"内的内容/输入。

无论我们将得到什么响应,我们都将显示在"#content"id 中。

我在这里做错了什么?

----编辑----

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

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

也许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 调用结果的更多详细信息。

这是jQuery中ajax对象的API。

重新创建您的设置,

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:提交.php

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

一切都很完美。

调试技巧:

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

2(确保您通过本地主机而不是文件路径访问带有javascript的HTML页面

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