[C#]使用 JSON ASMX Web 服务与 AngularJS

标签: json AngularJS C#
发布时间: 2017/2/23 20:06:47
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个问题,我需要我的数据库的数据显示在桌子上,但我不能。 我有在 Json,但当我尝试在一个表上显示显示我的数据库中的数据的 web 服务与角,它不会加载。

输入图像描述在这里

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <link href="Content/bootstrap.css" rel="stylesheet" />
    <script src="Scripts/jquery-1.9.1.js"></script>
    <script src="Scripts/bootstrap.js"></script>
    <script src="Scripts/angular.js"></script>
    <script src="FrontEnd/BI/Graficos/Compras/Forms/Controllers/ralatorio-compras.js"></script>
</head>
<body>

    <div class="container" style="padding-top: 100px" ng-app="appCompras">
        <table class="table table-striped table-bordered">
            <thead class="table-head center-content">
                <tr>
                    <th>Cod. Compra</th>
                    <th>Cod. Produto</th>
                    <th>Nome</th>
                    <th>Quantidade</th>
                    <th>Data da Compra</th>
                </tr>
            </thead>
            <tr ng-repeat="compra in names">
                <td>{{compra.PkCompras}}</td>
                <td>{{compra.IdProduto}}</td>
                <td>{{compra.Nome}}</td>
                <td>{{compra.Quantidade}}</td>
                <td>{{compra.DataCompra}}</td>
            </tr>
        </table>
    </div>


    <script>
        var app = angular.module('appCompras', []);

        app.controller('comprasCtrl', function ($scope, $http) {

            var url = "WebService.asmx\ListarQuinzeCompras";

            $http.get(url).success(function (data) {
                var appJson = JSON.parse(data);

                $scope.names = JSON.parse(appJson);
            })

        });
    </script>

</body>
</html>

我的 web 服务︰

[WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public void ListarQuinzeCompras()
        {
            var SQL = new LibOrgm.SQL();
            var cn = new ADODB.Connection();

            try
            {
                Context.Response.Clear();
                Context.Response.ContentType = "application/json";
                SQL.AbrirConexao(cn);

                var retorno = ComprasFO.ListarUltimasQuinzeCompras(cn);
                Context.Response.Write(SerializerFO.Serializador(retorno));
            }
            catch (Exception Ex)
            {
                Context.Response.Write(Ex);
            }
            finally
            {
                SQL.FecharConexao(cn);
            }
        }

我要序列化的类

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;

namespace WebApplication1.CORE
{
    public class SerializerFO
    {
        public static object DeserializarObjetoJson(string Json, object Objeto)
        {

            try
            {

                MemoryStream Stream = new MemoryStream(Encoding.Unicode.GetBytes(Json));
                DataContractJsonSerializer Desserializador = new DataContractJsonSerializer(Objeto.GetType());

                Objeto = Desserializador.ReadObject(Stream);

            }
            catch
            {
                throw;
            }
            return Objeto;
        }


        public static string Serializador(object Objeto)
        {
            string Json;
            try
            {

                MemoryStream Stream = new MemoryStream();
                DataContractJsonSerializer Serializador = new DataContractJsonSerializer(Objeto.GetType());

                Serializador.WriteObject(Stream, Objeto);

                Stream.Position = 0;
                StreamReader Leitor = new StreamReader(Stream);

                Json = Leitor.ReadToEnd();

            }
            catch
            {
                throw;
            }

            return Json;
        }
    }
}

我的类︰

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

namespace WebApplication1.BI.Graficos.Compras
{
    public class ComprasDA
    {
        public static ComprasBO GetCompras(int PkCompras, Connection cn)
        {
            var Compra = new ComprasBO();
            var RsCompra = new ADODB.Recordset();
            try
            {
                RsCompra.Open(String.Format("select * from Compras where PkCompras = {0}", PkCompras), cn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockReadOnly);
                if (!RsCompra.EOF )
                {
                    Compra.PkCompras = int.Parse(RsCompra.Fields["PkCompras"].Value.ToString());
                    Compra.IdProduto = int.Parse(RsCompra.Fields["IdProduto"].Value.ToString());
                    Compra.Nome = RsCompra.Fields["Nome"].Value.ToString();
                    Compra.Quantidade = int.Parse(RsCompra.Fields["Quantidade"].Value.ToString());
                    Compra.DataCompra = DateTime.Parse(RsCompra.Fields["DataCompra"].Value.ToString()); 
                }
            }
            catch (Exception)
            {
                throw;
            }

            return Compra;
        }


        public static ComprasBO[] ListUltimos15(Connection cn)
        {
            var RsCompras = new Recordset();
            try
            {
                RsCompras.Open("SELECT TOP 15 PkCompras FROM Compras ORDER BY PkCompras DESC", cn, CursorTypeEnum.adOpenStatic, LockTypeEnum.adLockReadOnly);
                if (!RsCompras.EOF)
                {
                    var ListaCompras = new ComprasBO[RsCompras.RecordCount];
                    for (int i = 0; i < RsCompras.RecordCount; i++)
                    {
                        ListaCompras[i] = GetCompras(int.Parse(RsCompras.Fields[0].Value.ToString()), cn);
                        RsCompras.MoveNext();
                    }
                    RsCompras.Close();
                    return ListaCompras;
                }

            }
            catch (Exception)
            {
                throw;
            }
            return new ComprasBO[0];
        }

    }
}

我的对象

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

namespace WebApplication1.BI.Graficos.Compras
{
    public class ComprasBO
    {
        public int PkCompras { get; set; }
        public int IdProduto { get; set; }
        public string Nome { get; set; }
        public int Quantidade { get; set; }
        public DateTime DataCompra { get; set; }
    }
}

解决方法 1:

您应该删除这样和替换数据,如下所示

        var url = "WebService.asmx\ListarQuinzeCompras";

        $http.get(url).success(function (data) {

            $scope.names = data;
        })
官方微信
官方QQ群
31647020