Returned custom error information in the Jquery ajax execution sequence (instance description)

Source: Internet
Author: User

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

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.