2

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.

whatever
  • 21
  • 4
  • take a simple step. Call you php server with a simple get request from a simple ajax cors call. That is a more simple approach to debug – Roninio Mar 31 '17 at 03:21
  • all GET / POST - Requests before the upload work totally fine. as i said, **all requests before uploading do have the cookie in request headers** and therefore those are all fine (Status 200 etc...) – whatever Mar 31 '17 at 06:46

1 Answers1

0

seems like server site error.

do you have below headers in your php files?

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
Roninio
  • 1,761
  • 1
  • 17
  • 24