CORS Cross-Origin Request

Make a cross-origin HTTP request with CORS headers inspection.

Code

Utilities
const response = await fetch('https://httpbin.org/get', {
  mode: 'cors',
  headers: {
    'X-Custom-Header': 'triggers-preflight'
  }
});

const data = await response.json();
const corsHeaders = {
  'access-control-allow-origin': response.headers.get('access-control-allow-origin'),
  'access-control-allow-methods': response.headers.get('access-control-allow-methods')
};
({ data, corsHeaders });
Browser·fetch() may be limited by CORS

CORS Modes

ModeBehavior
corsFull CORS request (default for cross-origin)
no-corsLimited response, no JS access to response
same-originFail if not same-origin

What Triggers Preflight

  • Custom headers (X-* headers)
  • Methods other than GET, HEAD, POST
  • Content-Type other than form-data, text/plain, application/x-www-form-urlencoded

More JavaScript Snippets