Understand the use of $.get, $.post, $.getjson, and $.ajax in jquery

Source: Internet
Author: User

Ajax 4 ways: $.get, $.post, $getJSON, $ajax.

1, $.get

The $.get () method uses the Get method to make an asynchronous request with the syntax structure:

$.get (URL [, data] [, callback])

Url:string type, the address of the AJAX request.

Data: Optional parameter, object type, Key/value data sent to the server is appended to the request URL as querystring.

Callback: Optional parameter, function type, which is called automatically when Ajax returns successfully.

Cases:

$.get(      "submit.aspx" ,     {       id: ‘123‘ ,       name: ‘syy‘ ,      },     function (data,state){       //这里显示从服务器返回的数据       alert(data);       //这里显示返回的状态       alert(state);      } )2, the $.post () $.post () method uses post to make an asynchronous request, which has a syntax structure of $.post (Url,[data],[callback],[type]) and $.get () usage, Only one more type parameter. Type:type is the requested data type, which can be a type of Html,xml,json, if we set this parameter to: JSON, then the returned format is in JSON format, if not set, and $.get () returns the same format, are all strings. Cases: $.post(      "submit.aspx" ,     {       id: ‘123‘ ,       name: ‘syy‘ ,      },     function (data,state){       //这里显示从服务器返回的数据       alert(data);       //这里显示返回的状态       alert(state);      },      "json" )3, $.getjson () $.getjson () is specifically for Ajax to get JSON data and is set to support cross-domain calls, the syntax of the format is: Getjson (Url,[data],[callback]) url:string type, Send Request Address data: Optional parameter, to be sent Key/value parameter, same as Get,post type of data callback: Optional parameter, load success callback function, same as Get,post type callback JSON is an ideal data transmission format, it can be well fused with JavaScript or other host language, and can be used directly by JS. Using JSON to send "naked" data directly through GET, post, is structurally more reasonable and more secure. As for jquery's Getjson () function, it's just a simplified version of the Ajax () function that sets the JSON parameter. This function can also be used across domains, with a certain advantage over get () and post (). In addition, this function can be used to write the request URL as a "myurl?callback=x" format, so that the program executes the callback function X. 4, $.ajax ()

$.ajax () is a common Ajax package in jquery, with the syntax in the following format:

$.ajax (Options)

Where options is an object type, it indicates the specific parameters of this Ajax call, and the most commonly used parameters are attached

$.ajax({     url: ‘submit.aspx‘ ,     datatype: "json" ,   //跨域可用jsonp     type: ‘post‘ ,     success: function (e) {   //成功后回调       alert(e);     },     error: function (e){    //失败后回调       alert(e);     },     beforeSend: function (){  /发送请求前调用,可以放一些 "正在加载" 之类额话       alert( "正在加载" );     } })

Understand the use of $.get, $.post, $.getjson, and $.ajax in jquery

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.