PM's Blog

Pramod Mohanan's notes about ASP.NET, MVC, C#, SQL, jQuery, Bootstrap

Cross domain Ajax Queries

Cross domain Ajax queries are not allowed as per w3c scripting standards and attempts to do that will result in browsers restricting such scripts to run raising exceptions with messages like

“Origin null is not allowed by Access-Control-Allow-Origin” or “Access to restricted URI denied”

A widely used work around to handle Cross domain Ajax queries is to create a proxy script at your server end. This proxy script actually fetches content from the remote domain and the client script will not be making any cross domain queries, instead does ajax queries to its own domain.

Another workaround that I discovered recently is to reference this request an external javascript to a remote domain like

<script src="http://somedomain.com/abc.aspx?q=ab" type="text/javascript"/>

this is allowed and in the above you see is referencing an aspx page instead of js. This aspx page can return data just like an ajax request.

To supress browser warinings it may be useful to change the MIME type of the server page to “text/javascript” like the following in a asp.net (c#) page

Response.ContentType = "text/javascript";

Leave a Reply

Your email address will not be published. Required fields are marked *

PM's Blog © 2018