服务端出现异常时,返回给客户端status仍然是ok的。因此在前端的catch或是error是得到不到服务端的throw异常信息的。

所以,你在服务端中,把异常时,也得作为成功执行返回给客户端。


你可以写一个类别:

Web API返回自定义数据给客户端

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Insus.NET.APIs
{
    public class Response
    {
        public bool ResponseStatus { get; set; }
        public string Message { get; set; }
        public object ResponseData { get; set; }

        public Response() { }

        public Response(bool status, string message, object data)
        {
            ResponseStatus = status;
            Message = message;
            ResponseData = data;
        }
    }
}
Source Code

相关文章: