如何将 angular 2 服务中的数组传递给 asp.net 网络 API?
How pass array in angular 2 service to an asp.net web API?
我在 angular 2 视图侧有复选框,当我单击其中一个复选框并单击按钮时,我将获得所选 checkbox.And 的值或我传递给服务的 id调用我的 asp.net Web API,这是我的观点
<div>
<label>
<input class="checkbox" type="checkbox" name="cbox1" value="1" >Armani
</label>
<label>
<input class="checkbox" type="checkbox" name="cbox1" value="2" >Versace
</label>
<button type="submit" id="select" class="btn btn-default btn-sm btn-primary">Apply</button>
</div>
在我的组件中
var self = this;
$(document).ready(function () {
$('#select').click(function () {
var seletedId = new Array();
$('input:checkbox.checkbox').each(function () {
if ($(this).prop('checked')) {
seletedId.push($(this).val());
}
});
if (seletedId.length > 0) {
self.getProductBrand(seletedId);
}
})
});
用这个我调用函数
getProductBrand(ids: Array<number>) {
this._productService.getProductBrand(ids).subscribe(data => {
console.log();
}, error => { this.errorMessage = error });
}
在网络中 API
[HttpGet]
[Route("api/user/brand/{id}")]
public Producttbl getProduBrand([FromUri] int[] id)
{
//
}
那么如何从 angular 2 传递数组以及如何在网络中获取 API?
通过上面的代码,它得到像 zone.js:2935 GET
这样的错误
http://localhost:58266/api/user/productbybrand/1,2 404 not found
尝试将值传递给 url,如下所示
http://localhost:58266/api/user/productbybrand/id=1&id=2
首先,在打字稿中创建数组
seletedIds = new Array();
然后将 jquery 数组分配给打字稿数组并传递到网络 API
var self = this;
$(document).ready(function () {
$('#select').click(function () {
var seletedId = new Array();
$('input:checkbox.checkbox').each(function () {
if ($(this).prop('checked')) {
seletedId.push($(this).val());
}
});
if (seletedId.length > 0) {
self.getProductBrand(seletedId);
self.seletedIds = seletedId;
console.log(self.seletedIds);
}
})
});
在我的网站 API 中更改 HttpGet 到 HttpPost 后,它就成功了。
我在 angular 2 视图侧有复选框,当我单击其中一个复选框并单击按钮时,我将获得所选 checkbox.And 的值或我传递给服务的 id调用我的 asp.net Web API,这是我的观点
<div>
<label>
<input class="checkbox" type="checkbox" name="cbox1" value="1" >Armani
</label>
<label>
<input class="checkbox" type="checkbox" name="cbox1" value="2" >Versace
</label>
<button type="submit" id="select" class="btn btn-default btn-sm btn-primary">Apply</button>
</div>
在我的组件中
var self = this;
$(document).ready(function () {
$('#select').click(function () {
var seletedId = new Array();
$('input:checkbox.checkbox').each(function () {
if ($(this).prop('checked')) {
seletedId.push($(this).val());
}
});
if (seletedId.length > 0) {
self.getProductBrand(seletedId);
}
})
});
用这个我调用函数
getProductBrand(ids: Array<number>) {
this._productService.getProductBrand(ids).subscribe(data => {
console.log();
}, error => { this.errorMessage = error });
}
在网络中 API
[HttpGet]
[Route("api/user/brand/{id}")]
public Producttbl getProduBrand([FromUri] int[] id)
{
//
}
那么如何从 angular 2 传递数组以及如何在网络中获取 API? 通过上面的代码,它得到像 zone.js:2935 GET
这样的错误http://localhost:58266/api/user/productbybrand/1,2 404 not found
尝试将值传递给 url,如下所示
http://localhost:58266/api/user/productbybrand/id=1&id=2
首先,在打字稿中创建数组
seletedIds = new Array();
然后将 jquery 数组分配给打字稿数组并传递到网络 API
var self = this;
$(document).ready(function () {
$('#select').click(function () {
var seletedId = new Array();
$('input:checkbox.checkbox').each(function () {
if ($(this).prop('checked')) {
seletedId.push($(this).val());
}
});
if (seletedId.length > 0) {
self.getProductBrand(seletedId);
self.seletedIds = seletedId;
console.log(self.seletedIds);
}
})
});
在我的网站 API 中更改 HttpGet 到 HttpPost 后,它就成功了。