Ways to circumvent Same-Origin Policy

suggest change

As far as client-side JavaScript engines are concerned (those running inside a browser), there is no straightforward solution available for requesting content from sources other than the current domain. (By the way, this limitation does not exist in JavaScript-server tools such as Node JS.)

However, it is (in some situations) indeed possible to retrieve data from other sources using the following methods. Please do note that some of them may present hacks or workarounds instead of solutions a production system should rely on.

Method 1: CORS

Most public APIs today let a page read a cross-origin response by enabling CORS (Cross-Origin Resource Sharing). The browser sends the request either way for a simple request. Script can read the response when Access-Control-Allow-Origin is * or the requesting origin. The header is one value, not a list of domains.

However, because developers cannot change other servers’ response headers, this method can’t always be relied on.

Method 2: JSONP

JSON with Padding is commonly blamed to be a workaround. It is not the most straightforward method, but it still gets the job done. This method takes advantage of the fact that script files can be loaded from any domain. Still, it is crucial to mention that requesting JavaScript code from external sources is always a potential security risk and this should generally be avoided if there’s a better solution available.

The data requested using JSONP is typically JSON, which happens to fit the syntax used for object definition in JavaScript, making this method of transport very simple. A common way to let websites use the external data obtained via JSONP is to wrap it inside a callback function, which is set via a GET parameter in the URL. Once the external script file loads, the function will be called with the data as its first parameter.

<script>
function myfunc(obj){
    console.log(obj.example_field);
}
</script>
<script src="http://example.com/api/endpoint.js?callback=myfunc"></script>

The contents of http://example.com/api/endpoint.js?callback=myfunc might look like this:

myfunc({"example_field":true})

The function always has to be defined first, otherwise it won’t be defined when the external script loads.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents