我在angularjs中使用asp.net web API,我正在调用返回数据表的Web API方法,但是有两倍的项目;例如如果datatable有2行,则响应有4行。
在该示例中,该方法应仅返回2行,但在客户端,它显示所有项目都在数组中重复。
如何在不获取重复数据的情况下正确检索它?
返回2行的Webmethod
//// GET api /
public HttpResponseMessage Get()
{
long id = 791;
DateTime dFromDate = DateTime.MinValue;
DateTime dToDate = DateTime.MinValue;
DataSet ds_jobs = DAL.Jobs.FilterJobByRecruiterId_new("", dFromDate, dToDate, "all", id, 1, 2, true, 2);
if (ds_jobs == null || ds_jobs.Tables.Count == 0 || ds_jobs.Tables[0].Rows.Count == 0)
throw new HttpResponseException(HttpStatusCode.NotFound);
return Request.CreateResponse<IEnumerable<DataRow>>(HttpStatusCode.OK, ds_jobs.Tables[0].AsEnumerable());
}
使用上述方法的AngularJS代码
var recruiter = angular.module('recruiter', []);
recruiter.service('jobsService', ['$http', function ($http) {
this.getAllJobsByRecruiterId = function () {
$http.get('http://localhost:51315/api/recruiter').success(function (result) {
return result[0].Table;
});
};
} ]);
recruiter.controller('jobsController', ['$scope', 'jobsService', function ($scope, jobsService) {
var recId = 791;
$scope.Jobs = jobsService.getAllJobsByRecruiterId();
} ]);
响应窗口(在浏览器中)有以下o / p
12个请求| 987 KB传输| 710毫秒(负载:651毫秒,DOMContentLoaded:640毫秒) HeadersPreviewResponseCookiesTiming
[{RowError:, RowState:2, Table:[{JobId:283499, JobStatus:Job Posted, RecruiterID:791,…},…],…},…]
0: {RowError:, RowState:2, Table:[{JobId:283499, JobStatus:Job Posted, RecruiterID:791,…},…],…}
HasErrors: false
**ItemArray: [283499**, Job Posted, 791, Technical Test Lead – Data Warehouse Testing, 1, 0, $, 50000, 80000, false,…]
RowError: ""
RowState: 2
Table: [{JobId:283499, JobStatus:Job Posted, RecruiterID:791,…},…]
**0: {JobId:283499,** JobStatus:Job Posted, RecruiterID:791,…}
**1: {JobId:283498**, JobStatus:Job Posted, RecruiterID:791,…}
1: {RowError:, RowState:2, Table:[{JobId:283499, JobStatus:Job Posted, RecruiterID:791,…},…],…}
HasErrors: false
**ItemArray: [283498**, Job Posted, 791, Technical Test Lead – Automation Testing using Ruby Cucumber, 1, 0, $,…]
RowError: ""
RowState: 2
Table: [{JobId:283499, JobStatus:Job Posted, RecruiterID:791,…},…]
**0: {JobId:283499**, JobStatus:Job Posted, RecruiterID:791,…}
**1: {JobId:283498**, JobStatus:Job Posted, RecruiterID:791,…}
============= 请参见结果集中每次重复2次的作业ID 283499,283498,即使数据表仅有2行。奇怪的行为!