Angular2 - http://localhost:4200/为什么附加了 api 调用? [英] Angular2 - http://localhost:4200/ being appended with api call why?

查看:22
本文介绍了Angular2 - http://localhost:4200/为什么附加了 api 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始学习 angular2 跟着 heroes 教程.我正在发出创建请求,URL 非常好,参数也很好.但我仍然很困惑为什么 http://localhost:4200/ 被附加到我的 API 调用中,因此 URL 完全改变,调用失败.请说明这个问题.我用谷歌搜索了很多,但找到了原因.

我的创建方法

 create(user: object): Promise

解决方案

您需要为 URL 添加协议.否则,它是一个相对 URL:

.post('http://localhost/usmanProject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0', user, { headers: this.headers })

just start learning angular2 followed the heroes tutorial. I am making a create request, the URL is perfectly fine, the parameters are fine. But I am still confused why http://localhost:4200/ is being appended with my API call, and because of that the URL gets totally changed, and the calls failed.please shed some light over this issue. I googled a lot but could find the reason.

My Create Method

 create(user: object): Promise<any> {
    return this.http
        .post('localhost/usmanProject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0', user, { headers: this.headers })
        .toPromise()
        .then(res => res.json().data)
        .catch(this.handleError);
}

解决方案

You need to add your protocol for your URL. Otherwise, it's a relative URL:

.post('http://localhost/usmanProject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0', user, { headers: this.headers })

这篇关于Angular2 - http://localhost:4200/为什么附加了 api 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆