【问题标题】:Didn't get json array in App js file没有在 App js 文件中获取 json 数组
【发布时间】:2015-10-10 09:46:52
【问题描述】:

我的服务代码如下所示:-

data.service('SmartLearnerService', function ($http) {

//Get Single Records
this.get = function (id) {
    return $http.get("/api/Category/");
    }
});

这是我的 App.js 控制器代码:-

$scope.questionlist = SmartLearnerService.get();

    $scope.questionlist.then(function (pl) {
        var res = pl.data;
        $scope.que = res.QuestionLabel;

    },
                 function (errorPl) {
                     console.log('failure loading Employee', errorPl);
                 });
    console.log($scope.questionlist);

这是 web api 控制器的控制器代码:-

public class CategoryController : ApiController
{
    CommonDb db = new CommonDb();
    public JsonResult Get()
    {
        var Result = db.GetQuestionById().ToList();

        string message = "No Data Found";

        if (Result.Count() != 0)
        {
            return new System.Web.Mvc.JsonResult()
            {
                Data = Result,
                JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet
            };
        }

        else
        {
            return new System.Web.Mvc.JsonResult()
            {
                Data = message,
                JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet
            };
        }
    }
   }
}

这里是 div 标签,我想使用 ng-repeat 指令绑定来自 json 结果的问题。

<div class="question" align="center">{{Questions.QuestionLabel}}</div>

我在控制器的 $scope.questionlist 中绑定 json 数组时遇到问题,我成功地从 web api 控制器获取 json 结果。

【问题讨论】:

  • questionlist 是一个承诺,并不适合绑定。您似乎在 que 中放入了一些数据,但我猜想 pl.data.QuestionLabel 没有像我期望的那样定义 pl.data 是一个数组
  • 好的,我已经这样做了,但它对我不起作用,现在应该是什么解决方案???
  • 提供响应数据的示例以及您希望如何呈现它
  • ok 我想通过这种方式返回 $scope.questionlist 中的 json 数据 :- $scope.questionlist = [ { "QuestionID": "1", "QuestionLabel": "为什么镜像经常稍微有点弯曲(凸)?”,“图像”:“zibra-crossing.jpg”,“正确”:“二”,“解释”:“问题一解释在这里”},....];

标签: javascript json angularjs routes angular-services


【解决方案1】:

好吧,如果我不得不猜测(这正是我正在做的事情),你希望在你的控制器中有这样的东西......

SmartLearnerService.get().success(function(questions) {
    $scope.questionList = questions;
});

或者,如果您不喜欢附加组件 success / error 回调

SmartLearnerService.get().then(function(response) {
    $scope.questionList = response.data;
});

在你的模板中

<div ng-repeat="question in questionList">
    <div class="question" align="center">{{question.QuestionLabel}}</div>
    <!-- and so on -->
</div>

这完全是假设您的 C# 控制器返回的 JSON 看起来像...

 [{
     "QuestionID": "1",
     "QuestionLabel": "Why are mirrors often slightly curved (convex) ?",
     "Image": "zibra-crossing.jpg",
     "Correct": "two",
     "Explaination": "Question one explaination is goes here"
 }, {
 ...
 }]

【讨论】:

  • 服务器响应状态为 500
【解决方案2】:

你可以试试这个吗?

SmartLearnerService
            .get()
            .success(function (data, status) {
                if (status === 200) {
                    //your code to process data is here
                }else{alert(status)}
            })
            .error(function (data, status) {
                //TODO: Use nice alert.
                alert('Server request failed with status ' + status + ' while getting area in the ' + $item.Name);
            });

您将获得您收到的状态代码,然后您可以相应地更改代码。

我在我的案例中采用的方法是使用 NewtonSoft 的 JsonConvert 进行序列化,然后返回 Json 对象的字符串而不是 Json 对象本身以提高速度。

【讨论】:

  • 猜测,我会说 OP 的 (C#) 控制器只返回状态 200
  • @Phil,返回的状态码可能不是 200。我必须对代码进行一些扭曲才能获得 200 状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
相关资源
最近更新 更多