简体   繁体   English

ajax成功回调不起作用

[英]ajax success callback not working

So I have this JavaScript which works fine up to the $.ajax({ . Then it just hangs on the loader and nothing happens. 所以我有了这个JavaScript,它可以在$.ajax({ 。然后它就挂在了加载器上,什么也没有发生。

$(function() {
$('.com_submit').click(function() {
    var comment = $("#comment").val();
    var user_id = $("#user_id").val();
    var perma_id = $("#perma_id").val();
    var dataString = 'comment='+ comment + '&user_id='+ user_id + '&perma_id=' + perma_id;
    if(comment=='') {
        alert('Please Give Valid Details');
    }
    else {
        $("#flash").show();
        $("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
        $.ajax({
            type: "POST",
            url: "commentajax.php",
            data: dataString,
            cache: false,
            success: function(html){
        alert('This works');
                $("ol#update").append(html);
                $("ol#update li:first").fadeIn("slow");
                $("#flash").hide();
            }
        });
    }
    return false;
}); 
});

Try replacing: 尝试更换:

var dataString = 'comment='+ comment + '&user_id='+ user_id + '&perma_id=' + perma_id;

with: 与:

var dataString = { comment: comment, user_id: user_id, perma_id: perma_id };

in order to ensure that the parameters that you are sending to the server are properly encoded. 为了确保要发送到服务器的参数正确编码。 Also make sure that the commentajax.php script that you are calling works fine and it doesn't throw some error in which case the success handler won't be executed and the loader indicator won't be hidden. 还要确保您正在调用的commentajax.php脚本运行正常,并且不会引发任何错误,在这种情况下,将不会执行成功处理程序,并且不会隐藏加载程序指示符。 Actually the best way to hide the loading indicator is to use the complete event, not the success . 实际上,隐藏加载指示符的最佳方法是使用complete事件,而不是success事件。 The complete event is triggered even in the case of an exception. 即使发生异常,也会触发complete事件。

Also use a javascript debugging tool such as FireBug to see what exactly happens under the covers. 还可以使用JavaScript调试工具(例如FireBug)来了解实际情况。 It will allow you to see the actual AJAX request and what does the the server respond. 它将允许您查看实际的AJAX请求以及服务器如何响应。 It will also tell you if you have javascript errors and so on: you know, the kinda useful stuff when you are doing javascript enabled web development. 它还会告诉您是否存在javascript错误等:您知道,当您进行启用javascript的网络开发时,这很有用。

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

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