【发布时间】:2021-06-03 09:59:47
【问题描述】:
我正在尝试在ionic 中使用php 在后端发出http post 请求,..
这是我的 ionic 代码:
let body = {
'data': ['data [0]', 'data [1]']
};
let options = {
headers : { 'Content-Type' : 'application/json' }
};
const ifSuccess = (response) => {
console.log(response);
this.data = response;
};
const ifFail = (response) => {
this.error = 'Oops! Une erreur au niveau du serveur...';
};
this.http.post('http://localhost/cantine-itu/test', body, options).subscribe(ifSuccess, ifFail);
这是后端(在 php 中):
Flight::before('json', function () {
header('Access-Control-Allow-Origin: http://localhost:8100');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET,PUT,POST,DELETE');
header('Access-Control-Allow-Headers: *');
});
Flight::route('POST /test', function(){
$data = Flight::request()->data;
Flight::json($data);
});
我收到一条错误消息:Access to XMLHttpRequest at 'http://localhost/cantine-itu/test' from origin 'http://localhost:8100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
(error-image)
【问题讨论】:
-
尝试将标题放在 php 文件的顶部。
-
我试过了,但没有任何改变......
标签: php ionic-framework cors preflight