CORS problem #1018
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Hello,
When using this library to upload files to my API I get:
Failed to load http://localhost:8000/index.php/api/resources/logo: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.My Code looks like this:
The initial OPTION requests returns 200 OK with:
To override the issue, I had to disable one line in file-uploader.class.js:
//xhr.withCredentials = item.withCredentials;Now files are being send to the API (Laravel).
The problem here is that the browser does not allow a combination of sending credentials and a wildcard origin. Its by design.
So the error message is "the value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'."
The CORS response should be:
So you have to change CORS response on the server side.
Changing code:
//xhr.withCredentials = item.withCredentials;will result that you will not send credentials. So it will not violate the wildcard + credentials combination.
You could also do this:
this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };so you don't need to change the ng2-file-upload source files.
Thank you. It did work for me when I changed the wildcard in the past, however, I am writing an application which will get deployed at thousands of sites with different host names. So it won't be possible to predict the hostname easily.
@hasanenalbana Maybe your application can inspect the host name from the request and set it in the CORS response. This should support any number of sites.
Thank this work for me!!!!
What if i need to sent an authkey with the file upload request, do i need to set to false the credentials ? because if i do i still get a not authorized response from my API endpoint.
Works for me. Thanks!
this.uploader.onAfterAddingFile = (file) => {
file.withCredentials = false;
};
This is wokirng for me