无法通过“ Content-Type”发送JSON对象:“ application / x-www-form-urlencoded

时间:2019-09-25 09:38:29

标签: javascript json rest post

有一个奇怪的问题。

我有一个对象

import { Component, ViewChild, ViewChildren } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent {
  @ViewChildren('status')
  public statusElements: any;

  public setStatusTo(name: string): void {
    this.statusElements.toArray().forEach(
      (status) => {
        status.nativeElement.innerHTML = name;
      }
    ) 
  }
}

我也使用了npm软件包 const otherParams = { hello: "world" }; const params = { payload: JSON.stringify(otherParams); apiKey: 1234, hmac: 13414 } querystring进行了编码

params

一切正常,但问题是// post method here url: "https://endpoint.com", method: "POST", query: querystring.stringify(params), headers: { "Content-type": "application/x-www-form-urlencoded" } 没有发送,这是payload无法编码querystring的问题吗?

form data

1 个答案:

答案 0 :(得分:0)

尝试使用

url: "https://endpoint.com",
method: "POST",
query: querystring.stringify(params),
headers: {
  "Content-type": "application/x-www-form-urlencoded","application/json"
}