Jquery ajax return value and no return value problems

Source: Internet
Author: User
Tags json

Commonly used ajac in jquery includes $. ajax (), $. post, $. get (), $. load.

Example

The code is as follows: Copy code

$. 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: Copy code

$ ("# 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: Copy code

$. 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: Copy code

$. 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: Copy code

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: Copy code

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: Copy code

$. 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: Copy code

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;
}
}

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.