Aurelia Aurelia获取客户端和JSON POST

Aurelia Aurelia获取客户端和JSON POST,json,aurelia,stringify,Json,Aurelia,Stringify,我有以下工作正常的代码: 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 => {

我有以下工作正常的代码:

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);
});
但是,如果将
body:JSON.stringify(auth)
行替换为
JSON(auth)
,我认为这是使用Aurelia JSON帮助程序对对象进行JSON序列化的正确方法,我的API会抛出错误的请求


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

函数调用JSON.stringify,但也向标题添加
内容类型:application/JSON
。我想知道为您抛出的错误是否是由于头已经存在,因为您正在显式添加它

再次尝试使用
json
,但这次删除修改标题以添加该内容类型的代码


有关该json函数的代码,请参见此处:

您的API是否与您的网站位于同一域?如果是这样,请尝试在配置函数中去掉绝对路径(https://)。我可以看到,由于某些原因,请求正文会变为空,带有绝对URL,而如果我发出本地请求,您的示例可以正常工作……不,API位于不同的URL,这确实给我带来了CORS问题,我在Safari中暂时禁用了该问题。请运行web代理(例如fiddler),然后查看那里到底发生了什么。然后将结果添加到您的问题中。你有没有看到过你的POST请求,或者仅仅是一个带有选项方法的POST请求?在ASP.NETWebAPI中,我在上面的代码中遇到了415(不支持的媒体类型)错误@Anj你很可能有CORS问题。使用“选项请求”,您的浏览器将尝试确定是否允许向外部域发送其他请求