如何使用此ajax请求防止数据丢失

时间:2015-11-12 09:31:07

标签: javascript php jquery ajax

我的博文末尾有一条评论表。到目前为止,在测试中,绝大多数时候评论都成功保存到数据库中,但是很有时候页面给人的印象是成功发布了评论 - 但是在重新加载页面后评论已经消失(并检查db表确认它从来没有做到那么远)。有没有办法修改我的代码以捕捉这些怪异的事件?

我知道$ .ajax有一个错误函数,但我不认为在这个实例中添加它会有所帮助。实际的ajax请求似乎正在起作用 - 因为它总是运行'success'函数中的内容。那么也许是postComment.php需要修改?

表单背后的代码提交:

navigator

postComment.php页面上的代码:

if( $(".blogPost").length ) {
    $(".commentForm").submit(function(e) {
        e.preventDefault();
    }).validate({
            submitHandler: function (form) {
                var url = window.location.pathname;
                var post_url = url.substring(url.lastIndexOf('/') + 1);
                $("input[name=post_url]").val(post_url);
                var formData = $(form).serialize();
                var post_id = $(".post").attr("id");
                $.ajax({
                    url:"/postComment.php?PostID=" + post_id,
                    type:"POST",
                    data: formData,
                    success:function(data){
                        $(".comments").prepend(data);
                        $("#commentName").val("");
                        $("#commentEmail").val("");
                        $("#commentWebsite").val("");
                        $("#comment").val("");
                        $(".commentForm input[type='submit']").val('Success!').delay(5000).queue(function(){
                            $(".commentForm input[type='submit']").val('Post Comment');
                        });
                    }
                });
            }
        });

}

3 个答案:

答案 0 :(得分:0)

首先确保插入成功然后只返回成功消息。

if(mysqli_query($link, $SQL)){
echo "<section class='comment'>
            <h3 class='commentAuthor'>$name$blogAuthor</h3>
            <a href='$website'><p class='commentAuthorWebsite'>$website</p></a>
            <p class='postDate'>$date</p>
            <p>$comment</p>
        </section>";
}else{
    echo "problem while inserting"; //or return an array with some status to tell the user to submit again.
// or header('HTTP/1.0 500 Internal Server Error'); exit;
    }

答案 1 :(得分:0)

确认插入没有任何问题:

$result = mysqli_query($link, $SQL);

if(!$result) {
    printf("Error: %s\n", mysqli_error($link));
} else {
    echo "<section ...

您应该阅读有关如何正确转义输入的信息:http://php.net/manual/en/mysqli.real-escape-string.php

答案 2 :(得分:0)

服务器端:

  • 检查SQL查询是否成功。

  • 根据您的查询的成功或失败,回显 true false

客户端:

$.ajax({

  url: "/postComment.php?PostID=" + post_id,
  type: "POST",
  data: formData,

  success:function(data){

    if(data == 'true') { // Prepend only If Successful

      $(".comments").prepend(data);

      $("#commentName").val("");
      $("#commentEmail").val("");
      $("#commentWebsite").val("");
      $("#comment").val("");

      $(".commentForm 

      input[type='submit']").val('Success!').delay(5000).queue(function() {

        $(".commentForm input[type='submit']").val('Post Comment');

      });

    } else { // Error

      alert("There was an issue in submitting your Comment. Please try again.");

    }

  }

});