身份验证错误:预检响应具有无效的HTTP状态代码405 [英] Authentication Error: Response for preflight has invalid HTTP status code 405
问题描述
我要实施身份验证.实际上,我的项目是在Angular 2 Beta中构建的,但是我正在使用此身份验证样品用于测试目的.我有自己的API,只是将其网址添加到此示例项目中.它不起作用)显示此错误:
I want to implement authentication. Actually, my project is being built in Angular 2 Beta, but I'm using this authentication sample for testing purposes. I have own API, and just added its url to this sample project. It didn't work) It shows this error:
Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
Fetch API cannot load http://blah-blah-blah. Response for preflight has invalid HTTP status code 405
这是登录方法:
login(event, phone, password) {
event.preventDefault();
window.fetch('http://blah-blah-blah', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
phone, password
})
})
.then(status)
.then(json)
.then((response:any) => {
localStorage.setItem('access_token', response.id_token);
this.router.parent.navigateByUrl('/home');
})
.catch((error) => {
alert(error.message);
console.log(error.message);
});
}
推荐答案
您似乎正在尝试进行跨域AJAX调用.为此,您的远程服务器API必须支持 CORS
.飞行前请求是特殊请求,浏览器在发出实际POST请求之前使用OPTIONS动词发送该请求:
It looks like you are trying to make a cross domain AJAX call. For this to work your remote server API must support CORS
. The pre-flight request is a special request that the browser sends using the OPTIONS verb prior to making the actual POST request:
OPTIONS http://blah-blah-blah
服务器必须以正常的200状态代码进行响应,并包括正确的CORS标头,以指示允许哪些域向其发出请求.只有在此之后,才会发送实际的POST请求.
The server must respond with a normal 200 status code and include the proper CORS headers indicating which domains are allowed to make requests to it. Only after that the actual POST request will be sent.
您的API的问题在于,端点向此OPTIONS
请求返回了405不允许状态代码.因此,您需要修复API,使其支持OPTIONS
动词.
The problem with your API is that the endpoint returned 405 Not Allowed status code to this OPTIONS
request. So you will need to fix your API so that it supports the OPTIONS
verb.
这篇关于身份验证错误:预检响应具有无效的HTTP状态代码405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!