简体   繁体   English

带有Jquery的Ajax-未执行回调

[英]Ajax with Jquery - Callback not executing

I have tried so many things, cannot figure this out, I am using this code, and I know that the start is working, because the script it connects to sticks some info in the db, but the callback never runs. 我已经尝试了很多事情,无法弄清楚,我正在使用此代码,并且我知道启动是可行的,因为它所连接的脚本会将一些信息粘贴到了db中,但是回调从未运行。

<script type="text/javascript">
$(document).ready(function() {
    $(document.body).on('click', "#reply_submit", function() {
            var formData = $('#reply').serialize();


            $.post("newpost.php",formData, function(data){
                    alert("hi");
             }, "json");
    });

}); });

My form's id is "reply" and the button I am using to submit it is "reply-submit", just to make sure those things are clear. 我的表单的ID是“ reply”,而我用来提交它的按钮是“ reply-submit”,只是为了确保这些内容清楚无误。

It also doesn't work if I remove that last "json" thing btw. 如果我删除最后一个“ json”,顺便说一句,它也不起作用。

If you read the docs for jQuery.post() , it says this about the callback: 如果您阅读了jQuery.post()的文档,则它说明了有关回调的内容:

success(data, textStatus, jqXHR) 成功(数据,textStatus,jqXHR)
A callback function that is executed if the request succeeds. 如果请求成功执行的回调函数。

If it's not getting called, it means that request did not succeed. 如果没有被调用,则意味着该请求未成功。 There is probably an error in newpost.php (sometime after the data insert, but before it would normally exit). newpost.php可能有一个错误(有时在数据插入之后,但通常不会退出之前)。

You can also catch error conditions by using the long-form jQuery.ajax() instead of the post shorthand: 您还可以通过使用长格式的jQuery.ajax()代替帖子的简写来捕获错误情况:

$.ajax({
    url: 'newpost.php',
    data: formData,
    type: 'post',
    dataType: 'json',
    success: function(data, textStatus, jqXHR) {
        alert('success');
    },
    error: function(jqXHR, textStatus, errorThrown) {
        alert('error!');
    }
});

When you click, the form is also being submitted the standard way. 当您单击时,表单也将以标准方式提交。 Modify your click handler like this: 修改您的点击处理程序,如下所示:

$(document).on('click', "#reply_submit", function(e) {
  e.preventDefault(); // prevent the default submit event
  var formData = $('#reply').serialize();
  // ...
});

Although I think document.body should be a valid node to wrap in jQuery, I've also modified it to the more commonly used document . 尽管我认为document.body应该是包装在jQuery中的有效节点,但我也将其修改为更常用的document

On that note, if the form is never destroyed by an Ajax event or other DOM modification, you could bind to #reply instead of document (or body ). 关于这一点,如果该表单从未被Ajax事件或其他DOM修改破坏,则可以绑定到#reply而不是document (或body )。

I'm simply assuming that you want to submit a form without reloading the whole page. 我只是假设您要提交表单而不重新加载整个页面。 Based on that assumption, following code will serve the purpose. 基于该假设,以下代码将达到目的。

$(document).ready(function(){

//on form submit event
$('form#reply').submit(function(){
    $.post('newpost.php',$(this).serialize(),function(){
        alert("Message or do something with the response data if you are expecting one");
    });

    //prevent default action
    return false;   
});

}); 

Please ignore the post if this is not the functionality you are looking for in your project. 如果这不是您在项目中寻找的功能,请忽略该帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM