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

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

问题描述

只需开始学习angular2,然后按照heroes教程进行操作即可.我正在创建创建请求,URL很好,参数也很好.但是我仍然感到困惑,为什么在我的API调用后附加http://localhost:4200/,因此URL完全改变了,并且调用失败了.请对此问题有所了解.我在Google上搜索了很多,但是找到了原因.

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.

我的创建方法

 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);
}

推荐答案

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

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天全站免登陆