Several use ajax

Abstract: ajax in several ways:

 

1, common wording, but also the most powerful writing:

. 1  $ .ajax ({
 2     
. 3       type: "the GET" , ----> herein may also be written "POST" request
 . 4      
. 5       URL: "SELECT" ,
 . 6     // Ajax request address   
. 7       dataType: 'JSON' ,
 . 8      
. 9       Data: {ID: 1001}, // may be a link string "id = 1001", recommended target 
10      
. 11       Success: function (Data) {
 12 is    
13 is  
14       IF (data.message === "Success" ) {
 15     }
 16       the console.log ( "the returned data:" + data);
 . 17     
18 is       }

 

 

2, $. Ajax () function is a tool frequently used

 When ajax submit data, usually submit a form, so serialized form data is very useful, such as:. $ ( "Form") serialize ()

. 1  $ .post (URL, formData,
 2         // $ .ajax () POST method shorthand, $ POST ( "URL request", "transmission of the data object", "successful callback", "return data type");   
. 3          function (data) {
 . 4           // return success, else can do a 
. 5                  
. 6            the console.log (data);
 . 7            },
 . 8            'JSON'); // data type returned
 . 9          
10      @ preventing form submission default behavior 
. 11          
12 is      return  to false 
13 is      
14 })

 

$ .Ajax () method shorthand GET and POST methods

. 1  $ .get (
 2      "URL" ,
 . 3      {userID: "123" },
 . 4      function (Response) {
 . 5          // callback method body 
. 6      }
 . 7  )
 . 8  
. 9  
10  $ .post (
 . 11      "URL" ,
 12 is      { userID: "123" },
 13 is      function (Response) {
 14          // callback method body 
15      }
 16 )

 


3.
1  // to get request form data acquired ajax json (only get request has json format, post no !!!!) 
2              / * $ .getJSON ( "URL", function (Data) {
 . 3  
. 4              }); * /

 



In fact, the result of more than a few ways to be obtained is the same, but the difference in form of writing, there is a difference between the post or get request request;

ajax post and get the difference (from: https://blog.csdn.net/qq_38499084/article/details/79699210 )

get and post requests are sent to the server for, but different transmission mechanisms 
1 GET request with parameters will be passed in the URL, rather POST request is sent to the WEB server as an entity of the content of the HTTP message. 
2. The first is "the data submitted by the GET method can only be 1024 bytes", the amount of data transmitted Post, you can reach 2M. 
3.get manner requested data is cached browser up so other people can read it from the browser history of these data, such as account passwords and so on. In some cases, get way will cause serious safety problems.

The post way, relatively speaking, avoid these problems. 
4.Post request must be set Content-Type is application / x-form-www-  urlencoded;
5. When the transmission request, Get Request parameter as a parameter in url, the thus transmitted send function is null, the request Post when the send method, but it needs to given parameters;

 

On the server side differences:

When using the client get request to the server using Request.QueryString acquisition parameters, while the client uses the post request to the server using Request.Form acquisition parameters. 
The HTTP standard contains two methods to achieve different purposes. POST is used to create content resources, content resources will be incorporated into the HTTP referrals. For example, the processing order form, to add a new row of data in the database and the like.

When a request no side effects (e.g., search), the GET method can be used; when requesting side effects (such as adding data lines), using the POST method. A more practical question is: GET method may produce a long URL,

You might exceed the limit of certain browsers and server URL length.

If either of the following case, the POST method is used:

  • Request results have persistent side effects, e.g., adding a new row of data in the database.
  • If you use the GET method, the data collected on the form may make URL too long.
  • Data to be transmitted than using 7-bit ASCII code.

If they meet any of these conditions, then use the GET method:

    • Request to find resources, HTML form data is only used to help search.
    • Request results without side effects sustained.
    • The total length of the data collection and input field names within the HTML form no more than 1024 characters.















Guess you like

Origin www.cnblogs.com/liuqijia/p/11437888.html