预检响应具有无效的 HTTP 状态代码 405 asp.net web api 和 Angular 2
Response for preflight has invalid HTTP status code 405 with asp.net web api and Angular 2
我试图将我的 asp.net 网络 api 与 angular 2.for 集成,以基本方式检查登录凭据。
asp.net 网页 api 代码是
public void postvalidUser(string UserName, string Password)
{
try
{
var login = uData.Users.Where(x => x.UserName == UserName).ToList();
if (login != null && login.Count > 0)
{
foreach (var log in login)
{
if (log.UserName == UserName && log.Password == Password)
{
Console.WriteLine("login ok");
}
}
}
else
{
Console.WriteLine("login fail");
}
}
catch (Exception ex){
Console.WriteLine(ex);
}
}
login.service.ts 文件为:-
@Injectable()
export class LoginService {
constructor(private _http: Http) { }
postvalidUser(UserName: string, Password: string) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({headers: headers });
const users = { UserName: UserName, Password: Password };
return this._http
.post("http://localhost:65440/api/User", users, options)
.map(result => result.json());
}
}
login.component.ts
export class LoginComponent implements OnInit {
constructor(private _loginservices: LoginService) { }
ngOnInit() {
}
OnClick(UserName: string, Password:string) {
this._loginservices.postvalidUser(UserName, Password).subscribe(users => console.log(users));
}
}
现在当我 运行 我的应用程序时,它向我显示错误
加载失败"my-url":预检响应具有无效的 HTTP 状态代码 405 && OPTIONS "my-url" 405(方法不允许)
请告诉我我的代码是否有误或遗漏了什么。
您必须在后端授权 OPTIONS 请求。
与授权 GET、POST 和 PUT 请求的方式相同,只需将 OPTIONS 添加到该列表即可。
如前所述,OPTIONS 是您的浏览器发送的预检请求,用于检查端点。
您的浏览器正在发送 OPTIONS
请求。
您需要创建一个具有相同路由的 HttpOptions
端点,或者创建一个拦截所有 HttpOptions
请求和 returns 200 请求的中间件。
我试图将我的 asp.net 网络 api 与 angular 2.for 集成,以基本方式检查登录凭据。 asp.net 网页 api 代码是
public void postvalidUser(string UserName, string Password)
{
try
{
var login = uData.Users.Where(x => x.UserName == UserName).ToList();
if (login != null && login.Count > 0)
{
foreach (var log in login)
{
if (log.UserName == UserName && log.Password == Password)
{
Console.WriteLine("login ok");
}
}
}
else
{
Console.WriteLine("login fail");
}
}
catch (Exception ex){
Console.WriteLine(ex);
}
}
login.service.ts 文件为:-
@Injectable()
export class LoginService {
constructor(private _http: Http) { }
postvalidUser(UserName: string, Password: string) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({headers: headers });
const users = { UserName: UserName, Password: Password };
return this._http
.post("http://localhost:65440/api/User", users, options)
.map(result => result.json());
}
}
login.component.ts
export class LoginComponent implements OnInit {
constructor(private _loginservices: LoginService) { }
ngOnInit() {
}
OnClick(UserName: string, Password:string) {
this._loginservices.postvalidUser(UserName, Password).subscribe(users => console.log(users));
}
}
现在当我 运行 我的应用程序时,它向我显示错误
加载失败"my-url":预检响应具有无效的 HTTP 状态代码 405 && OPTIONS "my-url" 405(方法不允许)
请告诉我我的代码是否有误或遗漏了什么。
您必须在后端授权 OPTIONS 请求。
与授权 GET、POST 和 PUT 请求的方式相同,只需将 OPTIONS 添加到该列表即可。
如前所述,OPTIONS 是您的浏览器发送的预检请求,用于检查端点。
您的浏览器正在发送 OPTIONS
请求。
您需要创建一个具有相同路由的 HttpOptions
端点,或者创建一个拦截所有 HttpOptions
请求和 returns 200 请求的中间件。