I'm using Ionic 3 and an own REST Api using Slim Framework with full access to the server.
Every API call (GET, POST, PUT and DELETE) despite of this:
let formData = new FormData();
formData.append("file",image);
return this.httpClient.post("https://myurl.com/api/v1/image,formData)
The imageURL is my server API and image is the path which I get from the cordova camera plugin.
This is my Server error:
[Error] Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.
[Error] Failed to load resource: Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.
[Error] XMLHttpRequest cannot load https://hobbyrea.com/api/v1/user/image due to access control checks.
This is what my server-side headers looks like:
$app->options('/{routes:.+}', function ($request, $response, $args) {
return $response;
});
$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
});
Can anybody give me an advice why I get the error with this API call?
EDIT:
I access the API from an iPhone.
EDIT 2: I use the cordova file-transfer plugin. Please keep in mind to upgrade the upload_max_filesize in the PHP settings. It would save you a lot of headache :D