JQuery Ajax instances ($.ajax, $.post, $.get)

Source: Internet
Author: User

jquery is very well encapsulated in asynchronous commits, and it's very cumbersome to use Ajax directly, and jquery greatly simplifies our operations, regardless of the browser's surprise.

Recommend a good jquery Ajax instance article, forget to go to see, the address is:http://www.cnblogs.com/yeer/archive/2009/07/23/1529460.html and/http www.w3school.com.cn/jquery/

$.post, $.get is a simple method, if you want to deal with complex logic, still need to use the Jquery.ajax ()

I. General format of $.ajax

$.ajax ({

Type: ' POST ',

URL: URL ,

Data: data ,

Success: Success ,

DataType: dataType

});

Second, the parameter description of $.ajax

Parameter description

Url Necessary. Specifies which URL to send the request to.
Data Optional. The map or string value. Specifies the data that is sent to the server along with the request.
Success (data, Textstatus, JQXHR) Optional. The callback function to execute when the request succeeds.
DataType

Optional. Specifies the data type of the expected server response.

The default is to perform smart judgments (XML, JSON, script, or HTML).

three, $.ajax need to pay attention to some places:

There are three main ways of 1.data, HTML splicing, JSON array, form form is serialized by Serialize (), and no intelligent judgment is specified by datatype.

2.$.ajax only submits form in text mode, if the asynchronous commit contains <file> uploads are not passed over, you need to use Jquery.form.js $.ajaxsubmit


Iv. $.ajax Examples of my practical application

JS Code
    1. 1.$.ajax asynchronous requests with JSON data
    2. var aj = $.ajax ({
    3. URL:' productmanager_reverseupdate ',//Jump to action
    4. data:{
    5. Selrollback:selrollback,
    6. Seloperatorscode:seloperatorscode,
    7. Provincecode:provincecode,
    8. Pass2:pass2
    9. },
    10. Type:' post ',
    11. Cache:false,
    12. DataType:' json ',
    13. Success:function (data) {
    14. if (data.msg = ="true") {
    15. //View ("Modified successfully!")    ");
    16. Alert ("modified successfully!    ");
    17. Window.location.reload ();
    18. }else{
    19. View (DATA.MSG);
    20. }
    21. },
    22. Error: function () {
    23. //view ("Exception!    ");
    24. Alert ("Exception!    ");
    25. }
    26. });
    27. 2.$.ajax asynchronous requests that serialize the contents of a table to a string
    28. function Notips () {
    29. var Formparam = $ ("#form1"). Serialize (); Serialize table contents as strings
    30. $.ajax ({
    31. Type:' post ',
    32. URL:' notice_notipsnotice ',
    33. Data:formparam,
    34. Cache:false,
    35. DataType:' json ',
    36. Success:function (data) {
    37. }
    38. });
    39. }
    40. 3.$.ajax asynchronous requests for stitching URLs
    41. var Yz=$.ajax ({
    42. Type:' post ',
    43. URL:' validatepwd2_checkpwd2?password2= ' +password2,
    44. data:{},
    45. Cache:false,
    46. DataType:' json ',
    47. Success:function (data) {
    48. if (data.msg = ="false") //The server returns false to change the value of ValidatePassword2 to Pwd2error, which is asynchronous and takes into account the return time
    49. {
    50. Textpassword2.html ("<font color= ' red ' > Business password is incorrect!    </font> ");
    51. $ ("#validatePassword2"). Val ("pwd2error");
    52. CheckPassword2 = false;
    53. return;
    54. }
    55. },
    56. Error:function () {}
    57. });
    58. 4.$.ajax asynchronous request to splice data
    59. $.ajax ({
    60. URL:' <%=request.getcontextpath ()%>/kc/kc_checkmernameunique.action ',
    61. Type:' post ',
    62. Data:' mername= ' +values,
    63. Async: false, //default is True async
    64. Error:function () {
    65. Alert (' error ');
    66. },
    67. Success:function (data) {
    68. $ ("#" +divs). HTML (data);
    69. }
    70. });

JQuery Ajax instances ($.ajax, $.post, $.get)

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.