zoukankan      html  css  js  c++  java
  • Web API返回自定义数据给客户端

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

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


    你可以写一个类别:

    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

    Web API实现数据返回给客户端。

     [HttpPost]
            public async Task<IHttpActionResult> GenerateQrCode(JObject jObj)
            {   
    
                Response response;
    
                try
                {
                    //WriteFile(qr, out f);
    
                    BarCode bc = new BarCode()
                    {
                        BackgroundColor = jsonParams.BackgroundColor,
                        ForegroundColor = jsonParams.ForegroundColor,
                        Width = jsonParams.Width,
                        Height = jsonParams.Height,
                        Content = jsonParams.Content,
                        FileName = f,
                        MineType = "image/png"
                    };
    
                    response = new Response();
                    response.ResponseStatus = true;
                    var list = new List<BarCode>() { bc };
                    response.ResponseData = list.FirstOrDefault<BarCode>();
                }
                catch (Exception ex)
                {
                    response = new Response()
                    {
                        ResponseStatus = false,
                        Message = ex.Message                    
                    };
    
                }
    
                return await Task.FromResult(Ok(response));
            }
    Source Code

    前端angularjs呼叫api:

     $http({
                    method: 'POST',
                    url: '/api/IoSvc/GenerateQrCode',
                    dataType: 'json',
                    headers: {
                        'Content-Type': 'application/json; charset=utf-8'
                    },
                    data: JSON.stringify(arg),
                })
                    .then(
                        function (response) {
                            var d = response.data
                            if (d.ResponseStatus) {
                                $scope.noImageHide = false;
    
                                $scope.FileName = d.ResponseData.FileName;
                            }
                            else {      
                                $scope.noImageHide = true;
                                $scope.FileName = "";
                                $window.alert(d.Message);
                            }
                        },
                        function (response) {
                            $window.alert(response.data);
                        }
                    )
                    .catch(function (response) {
                        $window.alert("catch:" + response.data);
                    })
                    //.finally(function () {
                    //    console.log("Task Finished.");
                    //})
                    ;
    Source Code
  • 相关阅读:
    正则表达式
    理解CPU steal time
    装饰器(带参数)
    装饰器(入门)
    递归
    冒泡算法
    Chrome for Mac键盘快捷键!来自Google Chrome官网!
    swoole深入学习 4. process
    通俗讲解 异步,非阻塞和 IO 复用
    swoole深入学习 3. upd Server和udp Client
  • 原文地址:https://www.cnblogs.com/insus/p/13533980.html
Copyright © 2011-2022 走看看