【发布时间】:2017-06-05 12:56:37
【问题描述】:
说明:
我正在使用LexikJWTAuthenticationBundle 并尝试使用从我的 Ionic2 应用程序发送的凭据生成 JWT 令牌。
使用 Curl 和 Postman 生成令牌可以正常工作。
curl -X POST http://localhost:8000/api/login_check -d _username=root -d _password=root
但是,Ionic2 在实际发送带有凭据的 POST 请求之前会发送一个预检 OPTIONS 请求。这被 Symfony3 误解并返回 404。
来自 Ionic2 的控制台错误:
选项http://localhost:8000/api/login_check 404(未找到) XMLHttpRequest 无法加载http://localhost:8000/api/login_check。
对预检请求的响应未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:8100” 使用权。响应的 HTTP 状态代码为 404。
0 - {"isTrusted":true}
例外:0 - {"isTrusted":true}
问题:
我必须在我的 Symfony3 应用程序中进行哪些更改(可能是我的 routing.yml 或 security.yml 中的某些内容)才能不发回 404?
代码:
Ionic2 HttpService:
@Injectable()
export class HttpService {
private baseUrl: string = "http://localhost:8000";
constructor(private http: Http){
}
public create<T>(url: string, extractData, data: any = null): Observable<Array<T>>{
let headers = new Headers({ 'Content-Type': 'application/json'});
let options = new RequestOptions({ headers: headers });
return this.http
.post(this.baseUrl + url, data, options)
.map(extractData)
.catch(this.handleError)
}
//More methods here...
}
Symfony routing.yml:
api_login_check:
path: /api/login_check
# This line is something I added to see if OPTIONS would work
# However, it did not work.
methods: [POST, OPTIONS]
类似问题:
【问题讨论】:
-
@suraj 感谢您的评论:),它引导我找到了正确的解决方案,但是博客已经过时了。那是 ionic 而不是 ionic2 的解决方案。
标签: typescript cors ionic2 symfony lexikjwtauthbundle