Angular 6 http POST - 将params设置为URL中的查询字符串

问题描述 投票:1回答:1

我有一个基于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是:'Content-Type':'application / json','Accept':'text / plain'

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

angular http-post angular6
1个回答
2
投票
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: ')
        }
      );
© www.soinside.com 2019 - 2024. All rights reserved.