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

标签 angular http-post angular6

我有一个基于 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/

相关文章:

Angular:如何以法语格式显示日期

datepicker - mat datepicker angular6中的周选择

angular - 使用整个 hmtl 字符串作为包含 css 和 js 的模板

angular - 动态创建 Angular 动画

java - 为什么我的 HttpPost 请求被分成两个请求?

java - 为什么上传图片到Blobstore后回调失败?

javascript - Angular:带有 *ngClass 的条件类

javascript - 做(点击)与订阅

c# - ASP.NET 文件上传

Angular 6 - 通过服务将消息从组件传递到消息组件