Angular 6 htttp POST - 将参数设置为 URL 中的查询字符串

Woj*_*ech 3 http-post angular angular6

我有一个基于 asp .net core 样板的应用程序。我需要从我的 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: ')
    }
  );
Run Code Online (Sandbox Code Playgroud)

我已经从 @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: ""**
Run Code Online (Sandbox Code Playgroud)

我的 httpHeaders 是: 'Content-Type': 'application/json', 'Accept': 'text/plain'

我的问题是如何在此服务中设置预期的 API POST 参数?

小智 5

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: ')
        }
      );
Run Code Online (Sandbox Code Playgroud)