我正在尝试在Angular4 Ionic应用程序中进行POST,我已经阅读了所有堆栈溢出的帖子,并做了他们说的话。在我的PHP中,我添加了
header ("Access-Control-Allow-Origin: *");
header ("Access-Control-Expose-Headers: Content-Length, X-JSON");
header ("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
header ("Access-Control-Allow-Headers: *");
我能够成功地通过POSTMAN进行发布,但是从android中的调试应用程序中我得到了...from origin 'http://localhost:8100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
我只是在angular4离子应用程序中尝试一个简单的帖子。
getImages() {
this.http.post(url, {
title: 'foo',
body: 'bar',
userId: 1
})
.subscribe(
res => {
console.log(res);
},
err => {
console.log("Error occured");
}
);
}
答案 0 :(得分:0)
我使用下面的php代码修复了这个问题,以专门允许来自我的本地主机,当我上线时,它将删除。
$ http_origin = $ _SERVER ['HTTP_ORIGIN'];
$allowed_domains = array(
'http://localhost:8100',
'localhost',
'*',
'localhost:8100',
'http://www.detdev.co.uk',
'detdev.co.uk'
);
if (in_array(strtolower($http_origin), $allowed_domains))
{
// header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Origin: $http_origin");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Authorization, Content-Type,Accept, Origin");
exit(0);
}