Ajax脚本不更改按钮文本

I have made a comment system in which i am inserting comments in my db through Ajax. Problem is, It adds the comments in db correctly but does not work with the success code for submit. Any suggestions please.

Form

 <h4>Add your Review:</h4>
  <?php
  if(isset($_SESSION["login_email"]) && !empty($_SESSION["login_email"]))
          {
        $email=$_SESSION['login_email'];
          ?>
        <div id="addCommentContainer">
        <form id="addCommentForm" method="post" action="">
         <div>
        <label for="body">Review</label>
         <textarea name="body" id="body" cols="20" rows="5"></textarea>
         <input type="hidden" name="email" id="email" value="<?php echo $email?>" />
        <input type="submit" id="submit" value="Submit" />
        </div>
        </form>
        </div>
             <?php
           }
     else{
       echo "Login to add review!";
        }
        ?>
 </div>

script.js

    $(document).ready(function(){
         var working = false;
        $('#addCommentForm').submit(function(e){

            e.preventDefault();
            if(working) return false;
            working = true;
            $('#submit').val('Working..');
            $('span.error').remove();

           $.post('submit.php',$(this).serialize(),function(msg){
          //This code isn't working 
                working = false;
                $('#submit').val('Submit');

                if(msg.status){
            $(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
                    $('#body').val('');
                }
                else {
                $.each(msg.errors,function(k,v){
                $('label[for='+k+']').append('<span class="error">'+v+'</span>');
                    });
                }
            },'json');

        });

    });

submit.php

 <?php

        // Error reporting:
        error_reporting(E_ALL^E_NOTICE);

        include "db/db.php";
        include "comment.class.php";

        /*
        /   This array is going to be populated with either
        /   the data that was sent to the script, or the
        /   error messages.
        /*/

        $arr = array();

        $validates = Comment::validate($arr);

        if($validates)
        {
            /* Everything is OK, insert to database: */

            mysqli_query($con," INSERT INTO comments(email,body,product_id)
                            VALUES (
                                '".$arr['email']."',
                                 '".$arr['body']."',
                                 '".$arr['productid']."'
                            )");


            $arr['dt'] = date('r',time());
            $arr['id'] = mysqli_insert_id($con);

            /*
            /   The data in $arr is escaped for the mysql query,
            /   but we need the unescaped variables, so we apply,
            /   stripslashes to all the elements in the array:
            /*/

            $arr = array_map('stripslashes',$arr);

            $insertedComment = new Comment($arr);


            /* Outputting the markup of the just-inserted comment: */

            echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));

        }
        else
        {
            /* Outputtng the error messages */
            echo '{"status":0,"errors":'.json_encode($arr).'}';
        }

        ?>

Make your submit button disable until you do not get response from server.

$(document).ready(function() {
    $('#addCommentForm').submit(function(e) {

        e.preventDefault();

        $('#submit').prop("disabled", true);

        $('span.error').remove();

        $.ajax({
            url: "submit.php",
            type: "POST",
            data: $('#addCommentForm').serialize()
            success: function(result) {

                $('#submit').prop("disabled", true);


                if (msg.status) {
                    $(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
                    $('#body').val('');
                } else {
                    $.each(msg.errors, function(k, v) {
                        $('label[for=' + k + ']').append('<span class="error">' + v + '</span>');
                    });
                }
            }
        });

    });

})

add .done, .fail, .always after your .post() and you will see errors. In the sample code below I am printing to the console. It seems like the issue is that you are requiring that your data returned is in JSON format, but you are not sending it in JSON. Convert your data to JSON, then send it:

 $.post('submit.php', $(this).serialize(), function (msg) {
     working = false;
     $('#submit').val('Submit');
     if (msg.status) {
         $(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
         $('#body').val('');
     } else {
         $.each(msg.errors, function (k, v) {
              $('label[for=' + k + ']').append('<span class="error">' + v + '</span>');
         });
     }

 }, 'json').done(function () {
     alert("second success");
 }).fail(function (a,b,c) {
     console.log(a);
     console.log(b);
     console.log(c);
 }).always(function () {
     alert("finished");
 });

UPDATE:

Make sure your submit.php is returning JSON like this:

echo json_encode($_POST);