Home > Article > Web Front-end > How to Fix \"Request Header Fields Not Allowed\" Errors in Cross-Origin File Transfers?
When attempting to send files via a POST request, developers may encounter the error: "Request header field Content-Type is not allowed by Access-Control-Allow-Headers." This error indicates that the server does not allow the specified header field.
The initial attempt to resolve this error by adding various headers, including "Access-Control-Allow-Origin" and "Access-Control-Allow-Methods," resulted in a new error: "Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers."
Investigating further, it was discovered that the jQuery documentation warns against setting the content type to anything other than "application/x-www-form-urlencoded," "multipart/form-data," or "text/plain" for cross-domain requests. Angular's default content type is "application/json," which triggers a preflight OPTIONS request to the server.
To resolve this issue, you can either overwrite Angular's default header or allow Access-Control-Allow-Headers on the server end. Here is an example of overriding Angular's default header:
$http.post(url, data, { headers : { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' } });
The above is the detailed content of How to Fix \"Request Header Fields Not Allowed\" Errors in Cross-Origin File Transfers?. For more information, please follow other related articles on the PHP Chinese website!