Jquery Ajax cannot obtain the returned value

Source: Internet
Author: User

I recently registered a member. The username is verified using jquery Ajax. Email, verification 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:

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

$. Get (URL, {async: false}, function (data) sets the transmission mode to synchronous transmission.

 

 

The final modification function is as follows. Test OK.

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

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.