1. execution sequence of multiple Ajax in one function
Because the default async value of Ajax in Jquery is true (asynchronous request), if you want to execute another Ajax after Ajax is executed, you need to set async to false.
Copy codeThe Code is as follows:
Function TestAjax (){
Var UserName = $ ("# txtUserName"). val ();
$. Ajax (
{
Url: "AjaxCheckUserName.htm ",
Async: false,
Success: function (data ){
Alert (data );
}
});
Alert ('test ');
$. Ajax ({
Url: "AjaxHandler. ashx ",
Async: false,
Data: "UserName =" + UserName,
Success: function (data ){
$ ("# DivAjax" pai.html (data );
},
Error: function (msg ){
Alert (msg. responseText );
}
});
}
2. Return custom errors
Set StatusCode = 500,
An error that triggers Ajax. Custom information is displayed on the received data.
Copy codeThe Code is as follows:
Error: function (msg ){
Alert (msg. responseText );
}
Context. Response. StatusCode = 500;
Context. Response. Write ("Enter the user name ");
Copy codeThe Code is as follows:
String sUserName = context. Request. QueryString ["UserName"];
If (! String. IsNullOrEmpty (sUserName ))
{
Context. Response. Write (string. Format ("Hello {0 }! ", SUserName ));
}
Else
{
Context. Response. StatusCode = 500;
Context. Response. Write ("Enter the user name ");
}
File Download:Download