我试图找出为什么我的JSON响应被方括号包围。我使用的是ASP.NET Web API和Angular。我认为这就是我的Angular代码没有打印到HTML的原因。
namespace GeekQuiz.Controllers
{
public class TrialController : ApiController
{
private TriviaContext db = new TriviaContext();
// GET api/trial
public IQueryable<TriviaQuestion> GetTriviaQuestions()
{
return db.TriviaQuestions;
}
}
}
以上代码来自我的TrialController
,我正在接听Angular的电话。
var app = angular.module('myApp', []);
app.controller('questCtrl', function ($scope, $http) {
$http.get("/api/trial").success(function (data, status, headers, config) {
$scope.options = data.options;
}).error(function (data, status, headers, config) {
$scope.title = "Oops... something went wrong";
$scope.working = false;
});
});
这是我的观看页面:
@{
ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
<div ng-app="myApp" ng-controller="questCtrl">
<table>
<tr ng-repeat="option in options">
<td>{{ option.id }}</td>
<td>{{ option.title }}</td>
<td>{{ option.questionId }}</td>
</tr>
</table>
</div>
<address>
One Microsoft Way<br />
Redmond, WA 98052-6399<br />
<abbr title="Phone">P:</abbr>
425.555.0100
</address>
<address>
<strong>Support:</strong> <a href="mailto:Support@example.com">Support@example.com</a><br />
<strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
</address>
@section scripts {
@Scripts.Render("~/Scripts/angular.js")
@Scripts.Render("~/Scripts/app/quest-controller.js")
}
我正在使用ASP.NET并尝试学习如何使用Angular和.NET构建一个restful应用程序。我正在使用本教程angular with ASP.NET和关于ASP.NET的Lynda.com课程。
这是我的JSON响应。如何乘坐方括号?我不知道他们为什么在那里。
[
{
"options":[
{"id":1,"title":"2000","questionId":1},
{"id":2,"title":"2001","questionId":1},
{"id":3,"title":"2002","questionId":1},
{"id":4,"title":"2003","questionId":1}],
"id":1,
"title":"When was .NET first released?"
},
{
"options":[
{"id":5,"title":"Contoso Ltd.","questionId":2},
{"id":175,"title":"System.DateTime","questionId":44},
{"id":176,"title":"System.Float","questionId":44}
],
"id":44,
"title":"Which of the following is NOT a value type in the .NET Framework Common Type System?"
}
]
以下是成功运行的JSON响应的结果:
{
"options":[
{"id":85,"title":"DOS","questionId":22},
{"id":86,"title":"Altair Basic","questionId":22},
{"id":87,"title":"PC Basic","questionId":22},
{"id":88,"title":"Windows","questionId":22}
],
"id":22,
"title":"What was Microsoft's first product?"
}
也许这会有所帮助:
namespace GeekQuiz.Models
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class TriviaQuestion
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
public virtual List<TriviaOption> Options { get; set; }
}
}
这段代码是TriviaQuestion模型,也许我没有正确对待它。 如果任何真棒的.NET程序员正在寻找门徒,请告诉我。
答案 0 :(得分:5)
因为parseSeries = do
spaces
titles <- title `sepBy` spaces
let nb = length titles
let parseRow = do
column <- digits
columns <- count (nb - 1) (spaces *> digits)
newline
return (column:columns)
dat <- many parseRow
return (titles, dat)
被序列化为JSON数组。
我认为IEnumerable<T>
是错误的,因为您从WebAPI返回数组本身,我无法找到理由认为您的选项在包含属性$scope.options = data.options;
的对象中被序列化,但似乎您的数据作为JSON数组返回 。
如果您将所谓的作业转为options
,那么您的约束可能会有效。