Home  >  Article  >  Web Front-end  >  Jquery ajax执行顺序 返回自定义错误信息(实例讲解)_jquery

Jquery ajax执行顺序 返回自定义错误信息(实例讲解)_jquery

WBOY
WBOYOriginal
2016-05-16 17:17:18914browse

1.多个Ajax 在一个function中的执行顺序

由于Jquery中的Ajax的async默认是true(异步请求),如果想一个Ajax执行完后再执行另一个Ajax, 需要把async=false就可以了.

复制代码 代码如下:

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

2. 返回自定义错误

设置 StatusCode = 500,

触发Ajax的错误(error), 在接收到数据显示自定义信息

复制代码 代码如下:

error: function (msg) {
                    alert(msg.responseText);
                }

 context.Response.StatusCode = 500;
 context.Response.Write("请输入用户名");


复制代码 代码如下:

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("请输入用户名");
            }

文件下载:download
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn