ng-repeat解析json的每个字符 [英] ng-repeat parsing every character of json

查看:55
本文介绍了ng-repeat解析json的每个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然尝试跟踪为什么我无法显示来自asp.net webmethod的json数据,但无法使用ng-repeat在表中显示-我已经明白了我的json存在问题,但是我看不到问题带有服务器json的表-我添加了一个索引列,似乎每个字符都添加了一行??

Still Trying to track down why I cannot display json data from asp.net webmethod won't display in table using ng-repeat - I've gotten to the point where it looks like there is an issue with my json but I don't see the problem The table with the server json - I added an index column and it seems to add a row per character ??

这是数据的屏幕截图-来自服务器的json数据和一个手动创建的角度集合,看起来像我的json数据

Here is a screenshot of the data - There is the json data from the server and a manually created angular collection that looks like my json data

感谢您的见解

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test1.aspx.cs" Inherits="SampleAngularjs.Test1" %>

<!DOCTYPE html>
<html  >

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
</head>

<body ng-app="codeapp">

  <div ng-controller="CodeController" >
    <button ng-click="doClick(item, $event)">Send AJAX Request</button>
    <br />
    Data from server: {{codes}}
    <br />
    Data from Manually built Collection: {{fields}}
    <br />

    <h3>And here is what you get if you just return the promise returned by $http.get():</h3>
    <pre>{{codes | json}}</pre>

     <br />
    <h3>Manually Built Angular Collection Table</h3>
    <table>
    <tr>
        <th>Code</th>
        <th>Desc</th>

    </tr>
    <tr ng-repeat="code in fields">
        <td>{{code.Code}}</td>
        <td>{{code.Desc}}</td>

    </tr>

   </table>

    <br />
    <h3>Server Json Table</h3>
    <table>
    <tr>
        <th>Row</th>
        <th>Code</th>
        <th>Desc</th>

    </tr>
    <tr ng-repeat="code in codes track by $index">
        <td>({{$index + 1}}) </td>
        <td>{{code.Code}}</td>
        <td>{{code.Desc}}</td>

    </tr>

   </table>

  </div>

   <script>
       angular.module("codeapp", [])
           .controller("CodeController", function ($scope, $http) {

               $scope.fields = [{ Code: "aaa", Desc: "aaa, desc" }, { Code: "bbb", Desc: "bbb, desc" }];

               $scope.codes = [];

               $scope.doClick = function (item, event){

                   $http.post('Test1.aspx/GetAllCodes', { data: {} })
                     .success(function (data, status, headers, config) {
                         $scope.codes = data.d;

                     })
                     .error(function (data, status, headers, config) {
                         $scope.status = status;
                     });

               }

           })
           .config(function ($httpProvider) {

               $httpProvider.defaults.headers.post = {};

               $httpProvider.defaults.headers.post["Content-Type"] = "application/json; charset=utf-8";

           });
  </script>


</body>

</html>

推荐答案

您正在从服务器获取一个字符串,并且对字符串进行ng-repeat会为您提供每个字符.尝试自己解析它是否起作用,然后查看请求和响应标头,以查看为什么它没有以 application/json :

You're getting a string from the server and ng-repeat over a string gives you each character. Try parsing it yourself to see if it works, then check out the request and response headers to see why it isn't sending the data back as application/json:

.success(function (data, status, headers, config) {
    $scope.codes = JSON.parse(data.d);
})

这篇关于ng-repeat解析json的每个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆