1.多个Ajax 在一个function中的执行顺序
由于Jquery中的Ajax的async默认是true(异步请求),如果想一个Ajax执行完后再执行另一个Ajax, 需要把async=false就可以了.
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
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 " ).html(data);
},
error: function (msg) {
alert(msg.responseText);
}
});
}
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 " ).html(data);
},
error: function (msg) {
alert(msg.responseText);
}
});
}
2. 返回自定义错误
设置 StatusCode = 500,
触发Ajax的错误(error), 在接收到数据显示自定义信息
error: function (msg) {
alert(msg.responseText);
}
context.Response.StatusCode = 500;
context.Response.Write("请输入用户名");
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
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( " 请输入用户名 " );
}
if ( ! string .IsNullOrEmpty(sUserName))
{
context.Response.Write( string .Format( " Hello {0}! " , sUserName));
}
else
{
context.Response.StatusCode = 500 ;
context.Response.Write( " 请输入用户名 " );
}
文件下载: Download