Home > Article > Web Front-end > ## Why am I getting a SyntaxError when using fetch API with \'no-cors\' mode despite expecting an opaque response?
Issue: An attempt to fetch an API with the mode set to 'no-cors' results in a SyntaxError in response to response.json().
Explanation:
When using the fetch API with 'no-cors' mode, the browser will silently fail any requests that require CORS permission. The error message "Unexpected end of input" is thrown because the fetch operation returns an opaque response (meaning its contents cannot be accessed).
Solution:
The "if an opaque response serves your needs" indicates that the server does not grant CORS permission to the request. To resolve the issue, you should:
Additional Note:
CORS (Cross-Origin Resource Sharing) protects against cross-site scripting (XSS) attacks by preventing unauthorized access to resources from different origins. Granting CORS permission should be done carefully to maintain security.
Reference:
The above is the detailed content of ## Why am I getting a SyntaxError when using fetch API with \'no-cors\' mode despite expecting an opaque response?. For more information, please follow other related articles on the PHP Chinese website!