There are many types of jquery ajax return values, such as html, text, json, and xml. These values can be directly obtained using anonymous functions in ajaxp, the following is an example of jquery ajax return values for your reference.
Commonly used ajac in jquery includes $. ajax (), $. post, $. get (), $. load.
Example
The Code is as follows: |
|
$. Post ("test. jsp ", {Name: "cssrain", time: "2008/01/21"}, // data to be transmitted Function (data ){ Alert ("returned data:" + data ); } ) |
The returned value is data. We only need to process the returned data in the function (data) {} anonymous function.
Example $. load ()
The Code is as follows: |
|
$ ("# Loadajax"). load ("http://www.111cn.net. post ", Function (responseText, textStatus, XMLHttpRequest ){ This; // here this points to the current DOM object, that is, $ (". ajax. load") [0] }); <Div id = loadajax> this way, you will receive the load return value. </div> |
Example
The Code is as follows: |
|
$. Ajax ({ Type: "get", // use the get method to access the background DataType: "json", // return data in json format Url: "BackHandler. ashx", // background address to be accessed Data: "pageIndex =" + pageIndex, // data to be sent Complete: function () {$ ("# load"). hide () ;}, // hide the loading prompt when the AJAX request is complete Success: function (msg) {// msg is the returned data. Bind The data here. Var data = msg. table; $. Each (data, function (I, n ){ Var row = $ ("# template"). clone (); Row. find ("# OrderID"). text (n. Order ID ); Row. find ("# CustomerID"). text (n. Customer ID ); Row. find ("# EmployeeID"). text (n. employee ID ); Row. find ("# OrderDate"). text (ChangeDate (n. Order Date )); If (n. date of shipment! = Undefined) row. find ("# ShippedDate"). text (ChangeDate (n. date of shipment )); Row. find ("# ShippedName"). text (n. goods owner name ); Row. find ("# ShippedAddress"). text (n. Cargo Master Address ); Row. find ("# ShippedCity"). text (n. Main City ); Row. find ("# more" rows .html ("<a href = OrderInfo. aspx? Id = "+ n. Order ID +" & pageindex = "+ pageIndex +"> & nbsp; More </a> "); Row. attr ("id", "ready"); // change the id of the row bound to the data. Row. appendTo ("# datas"); // Add it to the template container }); |
This is returned in json format.
Jquery ajax does not return values
In jquery's ajax method, after passing parameters, the callback determines whether there are success and error cases.
Sometimes, if no return value is required, the dataType: "json", parameter is set in the template format. When ajax passes the correct value, an error is reported when 200 is returned successfully.
I have not noticed the setting of the Data Type returned by the ajax method before. When there is no return parameter, you generally do not need to set the Data Type of the returned value. If an error occurs, an error is usually reported. At this time, the dataType: "json ",
Correct template for ajax method without return values:
The Code is as follows: |
|
$. Ajax ({ Type: "post ", Url: "index. php ", Data: "id =" + uid, Success: function (){ Alert (1 ); }, Error: function (){ Alert (0 ); } }); |
Example
Jquery's ajax authentication username. Email, verification code
The Code is as follows: |
|
Function check_email () { Var check_email = $ ("# reg_mail"). val (); Var reg =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((/. [a-zA-Z0-9 _-] {2, 3}) {1, 2}) $ /; Flag = reg. test (check_email ); If (flag) { Var email_value = $ ('# reg_mail'). val (); $. Get ("[var. base_url] register. php", {check_name: "email", check_value: email_value, async: false}, function (json ){ // Response ('{res_mail'{.html (json ); If (json = "OK ") { Certificate ('{res_mail'{.html ("<font color = 'green' font-size = '12'> <B> this email can be registered! </B> </font> "); Return true; } Else { Certificate ('{res_mail'{.html ("<font color = 'red' font-size = '12'> <B> this email has been registered! </B> </font> "); Return false; } }); } Else { $ ("# Res_mail" 2.16.html ("<font color = 'red' font-size = '12'> <B> enter the correct email address! </B> </font> "); Return false; } } |
It was written in the beginning. However, it is always impossible to obtain the returned status true or false and return an undefined. A post on csdn is classic:
The Code is as follows: |
|
Var boolean = false; $. Get (url, null, function (data) {// This is not difficult to understand, as long as you first know that the methods in jquery return jquery objects or the objects specified by jquery. // Ajax methods such as get and post of jquery are Asynchronous interaction by default, so they are returned before the get method is executed completely, in this case, bl is the defined bl = false; therefore, false is always returned; the value should be changed to synchronous. // If you want to return the correct bl value, you must change the get method. Generally, return values are not processed in ajax. // You can use $. data ("bl", bl); To save your value, and then use $. data ("bl. If (data. indexOf ("true")> = 0 ){ $ ("# Mid" Login .html ("Login successful "); Bl = true; $. Data ("bl", bl ); } Else { $ ("# Mid" ).html ("Incorrect username or password "); Bl = false; $. Data ("bl", bl ); } }); Return bl; |
In this way, the returned status can be obtained. Store values using the data method. And then obtain. Another way is
The Code is as follows: |
|
$. Get (url, {async: false}, function (data) sets the transmission mode to synchronous transmission. |
The final modification function is as follows. Test OK.
The Code is as follows: |
|
Function check_email () { Var check_email = $ ("# reg_mail"). val (); Var reg =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((/. [a-zA-Z0-9 _-] {2, 3}) {1, 2}) $ /; Flag = reg. test (check_email ); If (flag) { Var email_value = $ ('# reg_mail'). val (); $. Get ("[var. base_url] register. php", {check_name: "email", check_value: email_value, async: false}, function (json ){ // Response ('{res_mail'{.html (json ); If (json = "OK ") { Certificate ('{res_mail'{.html ("<font color = 'green' font-size = '12'> <B> this email can be registered! </B> </font> "); Tamp_email = true; $. Data ("tamp_email", tamp_email ); // $ ('# Sub_reg'). attr ("disabled", false ); } Else { Certificate ('{res_mail'{.html ("<font color = 'red' font-size = '12'> <B> this email has been registered! </B> </font> "); Tamp_email = false; $. Data ("tamp_email", tamp_email ); // $ ('# Sub_reg'). attr ("disabled", true ); } }); Return tamp_email; } Else { $ ("# Res_mail" 2.16.html ("<font color = 'red' font-size = '12'> <B> enter the correct email address! </B> </font> "); Return false; } } |