从web服务返回json数据

本文关键字:json 数据 返回 服务 web | 更新日期: 2023-09-27 18:08:20

    [WebMethod]
    public void contacters()
    {
        SqlConnection con = new SqlConnection(ConnectionString);
        List<object> obj = new List<object>();
        SqlCommand cmd = new SqlCommand("select * from authors", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        for(int i=0; i<ds.Tables[0].Rows.Count; i++)
        {
            obj.Add(ds.Tables[0].Rows[i][0]);
            obj.Add(ds.Tables[0].Rows[i][1]);
            obj.Add(ds.Tables[0].Rows[i][2]);
        }
        JavaScriptSerializer ser = new JavaScriptSerializer();
        var json = ser.Serialize(obj);
        Context.Response.Write("{"+'"'+"info"+'"'+";"+json+"}");
    }

这是我的Web服务

And My JS File

var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http) {
    $http.post("WebService2.asmx/contacters")
    .then(function (response) {
        $scope.names = response.data.info;
        console.log(response.data.info);
    });
});

My Code On ASPX testjson.aspx

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<div ng-app="myApp" ng-controller="customersCtrl"> 
<table>
  <tr ng-repeat="x in names">
    <td>{{ x.AuthorId }}</td>
    <td>{{ x.Fname }}</td>
      <td>{{ x.Lname }}</td>
  </tr>
</table>
</div>
    <script type="text/javascript" src="testjavascript.js"></script>
</asp:Content>
当我调试webservice时,

webservice正在显示数据,

但问题是,当我运行页面testjson.aspx数据不显示

有人能帮我一下吗?

web service运行正常,我想可能有一个

JS文件中的问题

从web服务返回json数据

试试这个:

public class ContactModel
{
    public int AuthorId { get; set; }
    public string Fname { get; set; }
    public string Lname { get; set; }
}
[WebMethod]
public void contacters()
{
    SqlConnection con = new SqlConnection(ConnectionString);
    List<ContactModel> obj = new List<ContactModel>();
    SqlCommand cmd = new SqlCommand("select * from authors", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        obj.Add(new ContactModel
                    {
                    AuthorId = (int) ds.Tables[0].Rows[i][0],
                    Fname = (string) ds.Tables[0].Rows[i][1],
                    Lname = (string) ds.Tables[0].Rows[i][2]
                    });
    }
    JavaScriptSerializer ser = new JavaScriptSerializer();
    var returnModel = new { info = obj };
    var json = ser.Serialize(returnModel);
    Context.Response.Write(json);
}

你的响应是序列化的,所以你需要在你的脚本中解析,试试这个

JSON.parse(data);

你也可以返回web服务作为字符串类型的静态方法,像下面的代码。

[WebMethod]
public static string contacters()
{
    SqlConnection con = new SqlConnection(ConnectionString);
    List<object> obj = new List<object>();
    SqlCommand cmd = new SqlCommand("select * from authors", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    for(int i=0; i<ds.Tables[0].Rows.Count; i++)
    {
        obj.Add(ds.Tables[0].Rows[i][0]);
        obj.Add(ds.Tables[0].Rows[i][1]);
        obj.Add(ds.Tables[0].Rows[i][2]);
    }enter code here
    JavaScriptSerializer ser = new JavaScriptSerializer();
    var json = ser.Serialize(obj);
    return json;
}