Aurelia aurelia-fetch-client和JSON POST

use*_*388 10 json stringify aurelia

我有以下代码,其工作正常:

import {inject} from 'aurelia-framework';
import {HttpClient, json} from 'aurelia-fetch-client';


@inject(HttpClient)
export class Items {
  heading = 'Items';
  apiKey = "";

  constructor(http) {
    http.configure(config => {
      config
        .useStandardConfiguration()
        .withBaseUrl('https://testme.com/api/')
          .withDefaults({
            headers: {
              'content-type': 'application/json',
              'Accept': 'application/json',
              'X-Requested-With': 'Fetch'
            }
          })
    });

    this.http = http;
  }

attach() {

let auth = {
  Username:"admin",
  Password:"1234"
};

return this.http.fetch('auth', {
      method: 'post',
      body: JSON.stringify(auth),
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      }
    })
    .then(response => response.json())
    .then(response => {
      this.apiKey = response.APIKey;
      console.log(response);
});
Run Code Online (Sandbox Code Playgroud)

然而,如果更换body: JSON.stringify(auth)与线json(auth)这是我认为正确的方式JSON序列化使用JSON奥里利亚帮助的对象,我的API抛出一个错误的请求.

帮助器与JSON.stringify有什么不同吗?

Tod*_*ice 5

json函数调用JSON.stringify,但也添加Content-Type: application/json到标头.我想知道为您抛出的错误是否可能是由于您已经明确添加它而已经存在的标头.

尝试json再次使用,但这次删除修改标题的代码以添加Content-Type.

请参阅此处获取该json函数的代码:https://github.com/aurelia/fetch-client/blob/master/src/util.js