Angular 2 HTTP POST执行OPTIONS调用

Gui*_*gui 13 angular

我的Angular 2应用程序遇到了一个非常奇怪的问题.我实际上想要向我的Play Scala API发出包含JSON的POST调用,但它仍然想尝试进行OPTIONS调用.

这是我的代码:

login服务

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)
}
Run Code Online (Sandbox Code Playgroud)

ApiEndpoint

constructor (private _http: Http) {}

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

然后当我尝试进行调用时(我已经尝试使用console.log来检查JSON并且它是正确的),并且调用尝试以任何原因进行OPTIONS调用:

Google请求图片

有人有想法吗?谢谢 !

Ami*_*ich 23

您正在进行跨域请求.

请求是localhost:9000和来自localhost:9002.

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

了解更多关于CORS 这里.