我的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)
}
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调用:
有人有想法吗?谢谢!
答案 0 :(得分:22)
您正在提出跨域请求。
请求是localhost:9000
,来自localhost:9002
。
浏览器使用OPTIONS动词创建一个飞行前请求,以便知道他是否可以继续并制作&#34;真实&#34;请求。
详细了解CORS here。