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

Source: Internet
Author: User

When we use JavaScript to write Ajax programs written "Happy", suddenly someone told you there is a thing called jquery, it will tell you not directly and HttpRequest is how happy, and you no longer need to worry tangled Ajax garbled problem, More happy is your JS code will be greatly simplified, after reading this article, you will find that Ajax, simply speaking is a word of things.

The focus of this article is to talk about 4 ways to call Ajax in jquery: $.get, $.post, $getJSON, $ajax. If the reader doesn't have the knowledge of JavaScript and jquery, or the concept of Ajax, ask Google boss First and then read this article.

1, $.get

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

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

Explain each parameter of this function:

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.

Finally write an example of $.get () for your reference:

1234567891011 $.get(    "submit.aspx",{        id:     ‘123‘,        name:   ‘青藤园‘,    },function(data,state){        //这里显示从服务器返回的数据        alert(data);        //这里显示返回的状态        alert(state);    })

2, $.post ()

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

$.post (Url,[data],[callback],[type])

This method is similar to $.get (), except that there is one more type parameter, so here is only the type parameter bar, other references above $.get ().

Type:type is the requested data type, 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 () return the same format, is a string.

Finally write an example of $.post () for your reference:

123456789101112 $.post(    "submit.aspx",{        id:     ‘123‘,        name:   ‘青藤园‘,    },function(data,state){        //这里显示从服务器返回的数据        alert(data);        //这里显示返回的状态        alert(state);    },    "json")

3, $.getjson ()

$.getjson () is set up to get JSON data specifically for Ajax, and supports cross-domain calls in the following syntax:

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, with 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 here I enclose some of the most commonly used parameters

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

Well, these are some of the ways jquery implements Ajax calls, and I hope it helps.

Http://www.cnblogs.com/ranzige/p/jquery_get_ajax.html

Deep understanding of 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.