简体   繁体   English

Angular 2 HTTP POST执行OPTIONS调用

[英]Angular 2 HTTP POST does an OPTIONS call

I'm encountering a really strange problem with my Angular 2 application. 我的Angular 2应用程序遇到了一个非常奇怪的问题。 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. 我实际上想要向我的Play Scala API发出包含JSON的POST调用,但它一直想尝试进行OPTIONS调用。

Here is my code : 这是我的代码:

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

ApiEndpoint ApiEndpoint

constructor (private _http: Http) {}

postLogin(body: string, options: any) {
    return this._http.post("http://localhost:9000/login", body, {
        headers: 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 : 然后当我尝试进行调用时(我已经尝试使用console.log来检查JSON并且它是正确的),并且调用尝试以任何原因进行OPTIONS调用:

Google请求图片

Has anyone an idea ? 有人有想法吗? Thanks ! 谢谢 !

You are making a cross domain request. 您正在进行跨域请求。

The request is to localhost:9000 and is made from localhost:9002 . 请求是localhost:9000 ,由localhost:9002

The browser creates a pre-flight request with the OPTIONS verb in order to know if he can continue and make the "real" request. 浏览器使用OPTIONS动词创建一个飞行前请求,以便知道他是否可以继续并发出“真实”请求。

Read more about CORS here . 在这里阅读更多关于CORS的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM