我正在尝试在Mac中调试离子应用程序:
$ ionic cordova emulate ios -- --buildFlag="-UseModernBuildSystem=0" --debug
在API SERVER(.NET)的请求之一中,出现错误:
Access-Control-Allow-Headers不允许请求标头字段Content-Type
login(login: LoginModel | null): Observable<LoginInfo | null> {
let url_ = this.baseUrl + "/API/Login/Login";
url_ = url_.replace(/[?&]$/, "");
const content_ = JSON.stringify(login);
let options_ : any = {
body: content_,
observe: "response",
responseType: "blob",
headers: new HttpHeaders({
"Content-Type": "application/json",
"Accept": "application/json"
})
};
return this.http.request("post", url_, options_).flatMap((response_ : any) => {
return this.processLogin(response_);
}).catch((response_: any) => {
if (response_ instanceof HttpResponseBase) {
try {
return this.processLogin(<any>response_);
} catch (e) {
return <Observable<LoginInfo | null>><any>Observable.throw(e);
}
} else
return <Observable<LoginInfo | null>><any>Observable.throw(response_);
});
}
答案 0 :(得分:0)
因此正确地在服务器中设置Access-Control-Allow-...
。
*
:HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "*");
正确的设置是:
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
答案 1 :(得分:0)
您可以在express.js
服务器中设置一些配置。
app.use(function(req, res, next) {
var allowedOrigins = [
'http://localhost',
'http://localhost:8000',
'http://127.0.0.1',
'http://127.0.0.1:8000',
'http://yourdomain.com',
];
var origin = req.headers.origin;
if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', true);
next();
});