【问题标题】:Angular 6 htttp POST - setting params as query string in URLAngular 6 htttp POST - 将参数设置为 URL 中的查询字符串
【发布时间】:2018-09-12 17:05:02
【问题描述】:

我有一个基于 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 和查询字符串参数,如: 产品代码:1111

但我的看起来像这样: http://localhost:21021/api/product/findProduct 并设置请求负载(始终为空):

**{rawParams: "", queryEncoder: {}, paramsMap: {}}
paramsMap: {}
queryEncoder: {}
rawParams: ""**

我的 httpHeaders 是: '内容类型':'应用程序/json', '接受':'文本/纯文本'

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

【问题讨论】:

  • 您的问题或您的问题之一是您有 ulr_,而 url_ 您从未指定您的服务器您在哪里指定 localhost?
  • 这是我在这篇文章中的缺失。但是谢谢;)
  • 如果我错了也请纠正我,但您似乎传递了一个空的 const 参数
  • 你确定这是一个帖子吗?听起来对我来说很重要,但我可能是错的
  • 这只是示例。我正在评估参数。而且,是的,它是 POST。我知道它会是 GET,但我不是后端开发人员 :)

标签: angular http-post angular6


【解决方案1】:
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: ')
        }
      );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2018-05-06
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多