Angular 2 HTTP POST进行OPTIONS调用 [英] Angular 2 HTTP POST does an OPTIONS call

查看:399
本文介绍了Angular 2 HTTP POST进行OPTIONS调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Angular 2应用程序遇到一个非常奇怪的问题.实际上,我实际上想对Play Scala API进行包含JSON的POST调用,但一直想尝试进行OPTIONS调用.

I'm encountering a really strange problem with my Angular 2 application. I actually want to make a POST call containing a JSON to my Play Scala API, but it keeps want to try to make an OPTIONS call.

这是我的代码:

LoginService

constructor (private _apiEndpoint: ApiEndpoint) {}

postLogin(login: string, credential: string): Observable<AuthToken> {
    let headers = new Headers({ "Content-Type": "application/json" })
    let jsonLogin = {"login": login, "password": credential}

    return this._apiEndpoint.postLogin(JSON.stringify(jsonLogin), headers)
                    .map(this._apiEndpoint.extractData)
}

ApiEndpoint

constructor (private _http: Http) {}

postLogin(body: string, options: any) {
    return this._http.post("http://localhost:9000/login", body, {
        headers: options
    })
}

然后,当我尝试进行调用时(我尝试用console.log来检查JSON并正确无误),并且该调用出于任何原因尝试进行OPTIONS调用:

And then when I try to make the call (I've tried to console.log to check the JSON and it's correct), and the call tries to make an OPTIONS call for whatever reason :

有人有想法吗?谢谢!

推荐答案

您正在提出跨域请求.

该请求是向localhost:9000发出的,并且是从localhost:9002发出的.

The request is to localhost:9000 and is made from localhost:9002.

浏览器使用OPTIONS动词创建飞行前请求,以便知道他是否可以继续并发出真实"请求.

The browser creates a pre-flight request with the OPTIONS verb in order to know if he can continue and make the "real" request.

此处了解更多信息.

这篇关于Angular 2 HTTP POST进行OPTIONS调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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