Detail the $. Ajax method submission form in jquery

  • 2020-03-30 04:15:04
  • OfStack


function postdata(){                        //Submit data function & NBSP;   < br / >     $.ajax({                                //Call jquery's ajax method & NBSP;   < br / >         type: "POST",                       //Set the form of the ajax method to submit the data & NBSP;   < br / >         url: "ok.php",                      //Submit the data to ok.   < br / >         data: "writer="+$("#writer").val(), //The value in the input box writer is the submitted data & NBSP;   < br / >         success: function(msg){             //The callback after a successful commit, the MSG variable is the content of the ok.php output.     < br / >             alert(" Data submission successful ");            //If necessary, you can display the value of the MSG variable in a DIV element & NBSP;   < br / >         }  
    });  

Description of jquery manual:
Data sent to the server. Automatically converts to the request string format. A GET request is appended to the URL. See the description of the processData option to disable this automatic conversion. Must be in Key/Value format. If it is an array, jQuery will automatically assign different values to the same name. Such as {foo: [" bar1 ", "bar2"]} into '& foo = bar1 & foo = bar2'.
Example:


$.ajax({  
    type: "POST",  
    url: "some.php",  
    data: "name=John&location=Boston",  
    success: function(msg){  
        alert( "Data Saved: " + msg );  
    }  
}); 

In this case, the parameters following the data can be written in two table forms: one is the way to write ordinary url parameters, and the other is to write them in json array.
The data section of the example above can also be written: data: {name:"John",location:"Boston"}. What is the difference between these two usages?

Subtle differences in their usage are found in development today. In the first case, we use url to pass parameters. If we add the symbol "&" in the parameter, the parameter may not receive or be incomplete. For example, "data: "name=John&location=Boston","

If the value of the name is "john&smith" this might be a problem, we can escape it with the encodeURIComponent() method in JS,

But if you write it in data: {name:"John",location:"Boston"}, you don't need to escape. If you escape, you will receive the escaped string


Related articles: