jpillora / xdomain

A pure JavaScript CORS alternative
https://jpillora.com/xdomain/
3.12k stars 269 forks source link

XDomain and ng-file-upload: Failed to execute 'setRequestHeader' on 'XMLHttpRequest' #157

Closed Donniewiko closed 9 years ago

Donniewiko commented 9 years ago

Hi,

I submitted this issue also to the ng-file-upload since it first occured since i added that module.

However, i narrowed the issue down to the following code:

https://github.com/jpillora/xdomain/blob/gh-pages/dist/xdomain.js#L470

for (header in _ref2) {
   value = _ref2[header];
   xhr.setRequestHeader(header, value);
}

The problemen is that ng-file-upload adds a header: __SetXhr (or something similar). Above code then tries to set the request header. However it fails to do so, since the _SetXHR is a function. In my particular case, i fixed the issue when i modified the code to:

for (header in _ref2) {
   value = _ref2[header];
   if (typeof value !== 'function') {
      xhr.setRequestHeader(header, value);
   }
}

Im not sure if i should mark this as a bug (with a proposed fix).

danialfarid commented 9 years ago

@Donniewiko I still think if xdomain XMLHttpRequest manipulation happens after ng-file-upload it should work without problem since xhr in that case will be the overwritten one from ng-file-upload which could handle setting a function as a request header. Could you create a jsfiddle for this? I could submit a PR after that to fix the issue

Donniewiko commented 9 years ago

Hi Danial,

I created an error version and a working version:

error: http://plnkr.co/edit/lJHMedHfRRqn4Q0bUJkF working: http://plnkr.co/edit/2tlEKTTEZyqp1otqCVmH

Please dont mind my coding. i created this quickly. But you will see the error in the console when you upload a file in the error version, and a result in the working version.

Kind regards

danialfarid commented 9 years ago

Ok I pushed a fix that would get rid of the error, but I don't think the upload progress would work. actually file progress should work too.