A standard ajax

Keywords: JSON Javascript Spring encoding

A standard ajax

Reader's words: after summarizing the past work, I found that I have made a lot of progress. In order to thank the fans for their support, I will strive to build a boutique blog, change the habit of making comments for myself. Again, I hope you can criticize and correct me. If you have solved your problem, remember to pay attention to it and praise it

Direct code:
<script type="text/javascript">
   function insertg(){
        var d = {};
        var t = $('#formid').serializeArray();
        $.each(t, function() {
          d[this.name] = this.value;
        });
       $.ajax({
           type:"post",
           async:true,
           url:"<%=basePath%>girls/insert.do",
           data: JSON.stringify(d),
           dataType: 'json',
           contentType:'application/json;charset=UTF-8',
           success:function(data){
            if(typeof result== "string"){
                        result = eval('(' + result + ')');
                    }
                    if (!result.success) {
                     //$. messager.alert("prompt", result.message, 'error');
                        } else {
                      //$. messager.alert("prompt", result.message, 'info');
                         location.reload()
                        }
                    }
       })
   }

</script>
Code interpretation: understanding different ajax

(1) ajax needs to stop
(2) Because spring MVC receive parameters are used in the background, dataType and contentType are used here to ensure that the parameters of ajax are json format and HTTP request specifies the HTTP content type + encoding of the response
(3) The following code block formid is the id of the form. The function of the code block is to obtain {playId:playId,playState:playState}
Of course, if you don't transfer the whole form, you can
Variable of type.

 var d = {};
        var t = $('#formid').serializeArray();
        $.each(t, function() {
          d[this.name] = this.value;
        });

(4) The JSON.stringify() method converts variables to corresponding json objects (as ajax request parameters).
(5) The following code block converts the ajax return result sequence to object type.

           if(typeof result== "string"){
                 result = eval('(' + result + ')');
            }

This is the blog

Posted by SoberDude on Sat, 02 May 2020 23:35:41 -0700