aurelia-http-client连接到错误的地址

问题描述 投票:1回答:2

我的aurelia Http客户端有问题。我的api(http // localhost:3000 / api / posts)运行正常。 get调用的输出(在邮递员或浏览器中)是:

[
  {
    "_id": "58a5f4f635c3ab643c74d97a",
    "text": "Foo",
    "name": "Fooo",
    "__v": 0
  },
  {
    "_id": "58a5fcc32586d0683455f78d",
    "text": "Bar",
    "name": "Baar",
    "__v": 0
  }
]

这是我在aurelia应用程序中的调用:

getPosts(){
 return client.get('http//localhost:3000/api/posts','callback')
  .then(data => {
    console.log(data);
    return data.response;
  })
}

And this is the output:正如你在图片中看到的那样,响应包含了“Aurelia”的内容,但是我的api从未触及aurelia,所以我认为URL有问题。

UPDATE1:

GManProgram(缺少:)建议的修复是问题所在。

UPDATE2:

我已经将客户端更改为aurelia-fetch-client,如GManProgram建议的那样。 Here is the new output我似乎把api的地址放在自己的地址后面。何我可以强迫它只使用api地址?

xmlhttprequest aurelia aurelia-cli
2个回答
2
投票

首先,在您发布的示例中,您在URL中的http之后缺少:字符。

如果这不能解决问题,并且你正在使用HttpClient中的aurelia-fetch-client,那么你可能想尝试使用.fetch方法而不是.get方法

http://aurelia.io/hub.html#/doc/api/aurelia/fetch-client/1.1.0/class/HttpClient

在你的情况下,因为看起来你期待json,典型的提取调用看起来像:

return this.httpClient.fetch('http://localhost:3000/api/posts')
  .then(response => response.json())
  .then(response => new CaseModel(response));

您还可以从aurelia-fetch-client导入json方法。

否则,可能已经在应用程序中使用基本URL配置了HttpClient并且它正在搞砸你?


1
投票

关于什么:

return client.get('posts','callback')
© www.soinside.com 2019 - 2024. All rights reserved.