我有一个基于 asp .net 核心样板的应用程序。 我需要从我的 Angular 6 应用程序发送 http POST 参数。
我的服务是这样的:
public findProduct(productCode: string) {
const url_ = 'api/product/findProduct';
const params = new URLSearchParams();
params.set('productCode', productCode);
return this.http.post(url_, params, httpHeaders)
.subscribe(
result => {
console.log(result);
},
error => {
console.log('There was an error: ')
}
);
我已经从@angular/http 导入了 URLSearchParams,但我仍然遇到同样的问题。我的 POST URL 不好,因为 API 期望这样的 POST URL: <强> http://localhost:21021/api/product/findProduct?productCode=1111 和查询字符串参数,例如: productCode: 1111
但是我的看起来是这样的: <强> http://localhost:21021/api/product/findProduct 并设置请求负载(始终为空):
**{rawParams: "", queryEncoder: {}, paramsMap: {}}
paramsMap: {}
queryEncoder: {}
rawParams: ""**
我的 httpHeaders 是: '内容类型':'应用程序/json', '接受':'文本/纯文本'
我的问题是如何在此服务中设置预期的 API POST 参数?
最佳答案
We have to pass params as 3rd parameter for post request
public findProduct(productCode: string) {
const url_ = 'api/product/findProduct';
const params = new URLSearchParams();
params.set('productCode', productCode);
return this.http.post(url_,,params, httpHeaders)
.subscribe(
result => {
console.log(result);
},
error => {
console.log('There was an error: ')
}
);
关于Angular 6 http POST - 将参数设置为 URL 中的查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52300257/