Angular Post请求变为选项请求
我的问题在我的api 6中。一旦在发送POST请求时创建了拦截器,这些拦截器就成为请求中的选项 有什么问题 这是一个服务的代码Angular Post请求变为选项请求,angular,typescript,request,Angular,Typescript,Request,我的问题在我的api 6中。一旦在发送POST请求时创建了拦截器,这些拦截器就成为请求中的选项 有什么问题 这是一个服务的代码 import { Injectable } from '@angular/core'; import { HttpClient, HttpClientModule } from '@angular/common/http'; import { AppConfig } from '../app.config'; import { Perfil } from '../Ent
import { Injectable } from '@angular/core';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { AppConfig } from '../app.config';
import { Perfil } from '../Entities/Perfil';
// import { Http } from '@angular/http';
@Injectable({
providedIn: 'root'
})
export class UsuariosService {
constructor(private http: HttpClient, private config: AppConfig) { }
private per = new Perfil(1);
getPerfiles() {
return this.http.post(this.config.apiUrl + 'perfiles/Buscar/', this.per);
}
}
此图像将帮助我们解决此问题:
问候 这是一个CORS问题,就像lomboboo说的那样,
选项
是向服务器发出的一个特殊请求,询问我可以做什么,我是否可以访问内容
为了提供修复,在您的服务器(后端)中,您需要实现CORS头并允许
POST
和您需要的任何其他操作。这是CORS问题,就像lomboboo所说的选项
是向服务器发出的一个特殊请求,询问我可以做什么以及我是否可以访问内容
//**need to pass header in your post api**
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
getPerfiles() {
return this.http.post(this.config.apiUrl + 'perfiles/Buscar/', this.per,httpOptions).subscribe(result => {
console.log(result);
}, error => console.log('There was an error: '));
};
}
为了提供修复,您需要在服务器(后端)中实现CORS头并允许
POST
和任何其他需要的操作。Yeahh。我知道他们是cors,并在后面实施,但很糟糕。
//**need to pass header in your post api**
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
getPerfiles() {
return this.http.post(this.config.apiUrl + 'perfiles/Buscar/', this.per,httpOptions).subscribe(result => {
console.log(result);
}, error => console.log('There was an error: '));
};
}
我实施的解决方案是:
(使用.Net Framework 4)
WebApiConfig.cs的内部
感谢lomboboo和Mike,这非常有帮助 是的。我知道他们是cors,并在后面实施,但很糟糕。 我实施的解决方案是: (使用.Net Framework 4) WebApiConfig.cs的内部
感谢lomboboo和Mike,这非常有帮助
OPTIONS
是当您试图从不同于服务器的域访问资源时的特殊请求方法。它涉及CORS。请在此处阅读更多信息-选项
是您尝试从不同于服务器的域访问资源时的特殊请求方法。它涉及CORS。请在这里阅读更多内容-我测试了这个选项,但在我的上下文中(Angular 6和.Net Framework)对我不起作用。非常感谢。我测试了这个选项,但在我的上下文(Angular 6和.Net Framework)中,它对我不起作用。非常感谢。