Ajax request details

Source: Internet
Author: User
Tags html header http post

I. Basic differences between get () and post ()

1. Get is to add the parameter data queue to the URL referred to by the Action attribute of the submission form. The values correspond to each field in the form one by one and can be seen in the URL. Post uses the http post mechanism to place fields in the form and their content in the HTML header and send them to the URL address referred to by the Action attribute. You cannot see this process.

2. For the get method, the server uses request. querystring to obtain the value of the variable. For the POST method, the server uses request. Form to obtain the submitted data. You can use request to obtain parameters in either of the two methods.

3. The data volume transmitted by get is small and cannot exceed 2 kb. The amount of data transmitted by post is large, which is generally not restricted by default. However, in theory, it varies with servers.

4. Low get security and high post security.

5. <form method = "get" Action = "A. asp? B = B "> with <form method =" get "Action =". ASP "> is the same. That is to say, when the method is get, the parameter list behind the action page is ignored, while <form method =" Post "Action =". asp? B = B "> unlike <form method =" Post "Action =" A. asp ">.

Ii. Differences between Ajax get and post

1. get method 2. the get method can be used to transmit simple data, but the size is generally limited to 1 kb, and the data is appended to the URL for sending (HTTP header transfer), that is, the browser attaches the form field elements and their data to the resource path in the request line according to the URL parameter format. The most important thing is that it will be cached by the client's browser, so that others can read the customer's data from the browser's historical records, for example, account and password. Therefore, in some cases, the get method may cause serious security problems. 3. 4. ajax sending request: if it is a GET request, the send parameter must be null or xhr. send (); 5. 6. you do not need to set xhr for GET requests. setRequestHeader (header, value) 7. 8. NOTE: If xhr. if the send (parameter) parameter is not null, it is automatically converted to the POST Request Method in Some browsers. getmethod (); Method 9. 10. instance: 11. 12. function getajax () {13. 14. // obtain the xhr object 15. vaR xhr = getxhr (); 16. // specify the request type 17. xhr. open ("get", "Main. JSP? Username = 123 ", true); 18. // send the request 19. xhr. send (null); 20. // process server response events 21. xhr. onreadystatechange = function () {22. // check whether the processing is complete and whether the server has been processed successfully! 23. if (xhr. readystate = 4 & xhr. status = 200) {24. alert (xhr. responsetext); 25 .} 26 .} 27 .} 28 .} 29. note 30 when using get. (1) For GET requests (or any request involving URL-based parameters), The passed parameters must be first processed by the (pre-encoding) encodeuricomponent method. for example, VAR url = "Update. PHP? Username = "+ encodeuricomponent (username) +" & content = "+ encodeuricomponent (content) +" & id = 1 "; 31. 32.2.post method: 33. when the POST method is used, the browser sends the form field elements and their data to the Web server as the entity content of the HTTP message, rather than as the URL address parameter, the amount of data transmitted in post mode is much larger than that transmitted in get mode. If it is a POST request send (parameter) parameter: The parameter can be null or xhr. send () | send (with parameters) post requests must set xhr when passing values. setRequestHeader (header, value) 34. 35. instance: 36. function postajax () {37. // obtain the xhr object 38. vaR xhr = getxhr (); 39. // specify the request type 40. xhr. open ("Post", "Main. JSP ", true); 41. // set the header information 42. xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded") 43. // send parameter 44. xhr. send ("username = 345 & pass = 123"); 45. // handle the event 46. xhr. onreadystatechange = function () {47. // check whether the processing is complete and whether the server has been processed successfully! 48. if (xhr. readystate = 4 & xhr. status = 200) {49. alert (xhr. responsetext); 50 .} 51 .} 52 .} 53. 54. note the following when using post: 55. (1 ). set the context-type of the header to application/X-WWW-form-urlencode to ensure that the server knows that there are parameter variables in the object. the setRequestHeader ("context-type", "application/X-WWW-form-urlencoded;") of the XMLHTTPRequest object is usually used ;"). Example: xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded") 56. (2 ). the parameter is a key-value pair with one-to-one correspondence between names and values. Each pair of values is separated by an ampersand. for example, VAR name = ABC & sex = Man & age = 18. Note that VaR name = Update. PHP? ABC & sex = Man & age = 18 and VAR name =? ABC & sex = Man & age = 18 is incorrect. 57. (3 ). the parameter is sent in the send (parameter) method, for example, xhr. send (name); if it is a get method, XMLHTTP directly. send (null); 58. 59. (4 ). server-side request parameters are differentiated between get and post. For the get method, $ username = $ _ Get ["username"]; for the POST method, $ username = $ _ post ["username"]; 60. 61. in short, the get method delivers a small amount of data, high processing efficiency, low security, and will be cached, whereas the POST method does not.

 

Ajax request details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.