Home  >  Article  >  Web Front-end  >  Detailed explanation of the $.ajax method in jquery to submit the form_jquery

Detailed explanation of the $.ajax method in jquery to submit the form_jquery

WBOY
WBOYOriginal
2016-05-16 16:32:121266browse

Copy code The code is as follows:

function postdata(){                                                                                                                                               $.ajax({                                                                                                             ​  Type: "Post", // Set the form of ajax method to submit data
         url: "ok.php",                                                                                                                                                                                                                                 ; data: "writer=" $("#writer").val(), //The value in the input box writer is used as the submitted data
Success: function(msg){ //Callback after successful submission, msg variable is the content output by ok.php.
alert("Data submitted successfully"); //If necessary, the value of the msg variable can be displayed in a DIV element
                                                                            });
}



jquery manual description:
data The data sent to the server. Will be automatically converted to request string format. Appended to the URL in GET requests. See the processData option description to disable this automatic conversion. Must be in Key/Value format. If it is an array, jQuery will automatically assign the same name to different values. For example, {foo:["bar1", "bar2"]} is converted to '&foo=bar1&foo=bar2'.
Example:



Copy code

The code is as follows: $.ajax({ Type: "POST",
URL: "some.php",
Data: "name=John&location=Boston",
Success: function(msg){
alert( "Data Saved: " msg );
}  
});



The parameters following data here can be written in two forms: one is written in the same way as ordinary URL parameters, and the other is written in a json array,
The data part of the above example can also be written like this: data: {name: "John", location: "Boston"}. What is the difference between these two usages?
Today I discovered the subtle differences in usage between the two during development. The first method is to use URL to pass parameters. If the symbol "&" is added to the parameters, the parameters may not be received or may be incomplete, such as "data: "name=John&location=Boston",",


If the value of name is "john&smith", there may be problems if you write it like this. We can use the encodeURIComponent() method in JS to escape,

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn