
Cross-origin access refers to a situation where JavaScript running under one origin tries to access resources from another origin. An origin is defined by three parts:
Only when all three are identical do two URLs count as the same origin.
Browsers enforce the Same-Origin Policy for security reasons. It prevents one origin from freely reading or manipulating resources from another origin, which helps reduce risks such as data theft and malicious cross-site attacks.
JSONP, or JSON with Padding, is an older cross-origin technique based on the fact that <script> tags are not restricted in the same way as XMLHttpRequest.
<script> tag and points it to a target URL.GETCORS, or Cross-Origin Resource Sharing, is the standard modern solution.
Origin header with a cross-origin request.Access-Control-Allow-Origin.The server can also define allowed methods, headers, and whether credentials are permitted.
postMessage is an HTML5 API for secure communication between windows, tabs, and iframes, including cross-origin ones.
postMessage(data, targetOrigin).message event.event.data and validates event.origin.This is especially useful for iframe communication and embedded widgets.
WebSocket can also be used in cross-origin scenarios and is well suited for real-time communication.
Upgrade: websocket.101 if it accepts the protocol upgrade.This is commonly used for chat, collaboration tools, live dashboards, and games.
document.domain + iframe This is an older technique that only works for subdomains under the same top-level domain.
document.domain to the same top-level domain.This approach is limited and rarely recommended in modern systems.
window.name window.name survives full page navigations within the same tab, which makes it usable for a legacy cross-origin data handoff pattern.
window.name.window.name value.This technique is simple but outdated and limited.
location.hash The hash portion of the URL can also be used for lightweight cross-page messaging.
location.hash.This is only suitable for very small payloads and narrow use cases.
Using a Node.js server as a proxy is a common and practical server-side solution.
Because the browser is now talking to your own server, the request no longer violates the Same-Origin Policy.
Nginx can solve the same problem at the reverse-proxy layer.
This is often easier to operate in production than maintaining a custom proxy service.
CORS Anywhere is an open-source reverse proxy that adds CORS headers to forwarded responses.
It is convenient for quick experiments, but you should not treat it as a long-term architecture by default.
GET, weaker securityIn most modern applications, prefer CORS when you control the server. Use a reverse proxy when you do not want the browser talking to the target origin directly. Reach for WebSocket when the workload is real-time. The older techniques are mostly interview knowledge or historical context now.