我有一个api,它返回两个在邮递员中验证并使用开发人员工具响应的数组
c#
public async Task<IHttpActionResult> GetUsersAcccountContract(string username)
{
var users = await (
from u in db.users
select new { u.account_number,
u.contract_number }).ToListAsync();
return Json(users);
}
我用角度ngOnInit称呼它
this.sub = this.aroute.params.subscribe(params => {
if(currentUser.username != null) {
let apiURL = this.systemSettings.getWebAPISettings('contractNumber');
let token = this.userService.getToken();
// Setup the header with the auth token
let headers = new Headers({
'Authorization': 'Bearer ' + token
});
let urlOptions = new RequestOptions({ headers: headers, params: {
username: currentUser.username
}});
this.http.get(apiURL, urlOptions).subscribe(res => {
this.contractnumbers = res.json();
});
}
});
模型默认值:
contractnumbers:ContractNumbers = {
account_number: null,
contract_number: null
}
问题是当我在OnSubmit上使用它们时,我的账户和合同号始终得到空值
this.contractnumbers.account_number始终为空
为什么?
答案 0 :(得分:0)
请检查contractnumbers
的初始化位置。似乎您尝试访问它的位置没有任何价值。它是在组件类中声明和初始化的吗?
您应该可以使用HttpParam发送查询参数。
const params = new HttpParams()
.set('account_number', 'value here')
.set('contract_number', 'value here');
return this.httpClient.get(`${apiURL}`, { headers, params })