I am getting an error "401 Unauthorized" in Chrome and Firefox, when uploading files with angular 2 CLI to an apache2-server. The backend of the server is in PHP. I've tried it with three different node modules, all behave the same. I'm getting the error already on the OPTIONS-preflight,
"XMLHttpRequest cannot load (uploadurl) Response for preflight has invalid HTTP status code 401".
This error only happens, if im requesting from another source (e.g. "localhost:4200"). But when executing this project on the apache server, the upload works fine.
So on localhost: The difference is, all requests before uploading do have the cookie in request headers. But not the upload-request. here's an image: headers on the upload-request
I've tried theese three Node Modules so far:
- ng2-file-upload
- angular2-http-file-upload
- ng2-uploader
"withCredentials" on the uploader itself (e.g. for the ng2-file-upload from Valor Software ng-2-file-upload) is set to true.
I also tried already setting the header X-Requested-With: 'XMLHttpRequest'
.
And i already tried setting an Authorization: Basic
-header....Still the same issue.
When setting the Cookie:
-header myself OnBeforeFileUpload, of course i get an error message
"refused to set unsafe header Cookie"
would be cool, if someone knows how to fix this. I just looked everywhere for an answer but nothing did help.
EDIT: Theese headers are also set in the php backend:
header("Access-Control-Allow-Origin: http://localhost:4200");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
still got the error.