Origin 'url' is not allowed by Access-Control-Allow-Origin Origin 'url' is not allowed by Access-Control-Allow-Origin json json

Origin 'url' is not allowed by Access-Control-Allow-Origin


Ajax requests are limited by the browser's Same Origin Policy. In a nutshell, that means that you can't talk directly to a server via ajax that isn't on the same domain as the page your script is running in. So, unless you're developing a page for google.com, you can't talk to google.com directly.

There are work-arounds for this limitation involving the insertion of script tags (JS files loaded via script tags are not subject to the same origin policy) and then using JSONP callbacks to communicate data results back to your main script from those script tags. That is probably what you need to do here if the API you're attempting to use supports it.

jQuery will help you a lot here as it can automatically turn an ajax call into a JSONP call that is loaded via script tags and can work in cross domain situations. According to the jQuery doc for it's ajax function, it will do this automatically if it sees "callback=" in the parameter string for the ajax call or if you set the crossDomain option.


Edit
I thought it was clear what the problem was but it seems it might not be so here goes. This error you're seeing is the server restricting your domain from accessing it's resources via ajax requests. This is standard JavaScript security - your script can only talk to the domain it originated from. Since your JavaScript was not loaded from Google's domains, it's not in the list of domains allowed to access the calculator API via ajax and that's why you see this error message.

Options for making cross domain requests with jQuery are outlined here. As I mentioned earlier, JSONP will only be a valid option if the server supports it because it must send back appropriately formatted JSON.


It might help if you provided links to the pages that you're referring to.

From the looks of things though, this API doesn't support JSONP (unless there is an undocumented parameter) which is pretty much your only option for cross-domain ajax requests in this case since you don't control the server and can't change the access control headers.

You might want to consider building a server-side resource that will access this API for you without being constrained by the JavaScript security model such as the PHP script here.


It seems from this link - http://api.jquery.com/jQuery.ajax/ - which was provided earlier by jfriend00 - explains a parameter you can include in the JQuery ajax request called "crossDomain" which is a boolean.

crossDomain (default: false for same-domain requests, true for cross-domain requests)Type: BooleanIf you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true. This allows, for example, server-side redirection to another domain. (version added: 1.5)

Therefore setting it to true should solve(?) this. I am not an expert, but I tried it after continuously running into this issue and it seemed to resolve the problem.

Example:

$.ajax({ //my ajax request        url: "_URL_I_AM_MAKING_REQUEST_TO_",        type: "GET",        cache: false,        dataType: "json",        **crossDomain: true,**        data: { _mydata_        success : function(response){}});