Ajax settings Async To verify that the user name exists in the implementation method _ajax related

Source: Internet
Author: User

Add a user, need to determine whether the phone number exists, the first idea is very simple in the TextBox set up a onmouseout event, in IE has a good embodiment, but in Google is not very good

OK, in a different way, when submitting the form to check:

//check whether the mobile phone number exists function checkrepeat () {var id = ' ${item.id} ';
	var mobile = $ ("#mobile"). Val ();
	alert (ID); if (id==null| |
			id== ') {$.ajax ({url: '/admin/adminuser/ajaxcheckreapet.shtml?mobile= ' +mobile, type: ' Get ', DataType: ' Text ',
				Cache:false, Async:false, timeout:5000, Error:function () {alert (' Data acquisition failed! ');}, Success:function (msg) {
					if ("1" ==msg) {$ ("#spMobile"). attr ("style", "display:block;color:red;");
				$ ("#hiddenMobile"). attr ("value", "true");
					}else{$ ("#spMobile"). attr ("style", "display:none;");
				$ ("#hiddenMobile"). attr ("value", "false");
		
	}
				
			}
		});
return true;
		The function Save () {if (Checksimg () &&checkrepeat ()) {var hiddenmobile = $ ("#hiddenMobile"). Val ();
		alert (hiddenmobile);
			if (hiddenmobile== ' false ') {if ($ ("#form1"). Form ("validate")) {$ ("#form1"). Submit (); }
			
		}
	}
}

And then find something interesting in this process: when Hiddenmobile returns false, the form still submits

Ah, tangled ah, again to move the brain, FK

I think AH think, think Ah, looking for ah, suddenly thought of async, although before the use of this thing, but blindfolded it, added a async:false, I rub, again, when the demo, incredibly useful

Cache:false,
Async:false,

Well, to solve the problem, and then to further study a: hem, and harvest slightly, see for yourself

Async The default setting value is True, which is asynchronous, that is, when Ajax sends a request, the foreground will continue to execute the script behind the AJAX block until the server side returns the correct result before the server ends up executing the success. In other words, two threads are executed at this time, and the Ajax block makes a request after a thread and the script (another thread) behind the AJAX block

$.ajax ({ 
     type: "POST", 
     URL: "Venue.aspx?act=init", 
      dataType: "html", 
     success:function (Result) {  //function1 ()
       F1 (); 
       F2 (); 
    } 
     Failure:function (Result) { 
      alert (' Failed '); 
     }, 
 } 
 function2 (); 

In the example above, when the Ajax block makes a request, he stays function1 () and waits for the server to return, but at the same time (during the wait), the foreground executes function2 (), that is, there are two threads at this time, We are here to say Function1 () and function2 ().

When the Asyn is set to False, it is synchronized when the AJAX request is made, that is, when the Ajax block makes a request, he waits in the function1 () and does not perform function2 (), knowing that the function1 () part of the execution is complete.

Attention

Synchronization means that when the JS code is loaded into the current Ajax will put all the code in the page to stop loading, the page out of suspended animation state, when this Ajax execution will continue to run the other code page suspended animation state.

Asynchronous, the Ajax code runs the same way that other code can.

The Async:false of jquery, this attribute

The above Ajax settings Async Verify the existence of the user name is the implementation of small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.