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

前端 未结 4 1527
忘掉有多难
忘掉有多难 2020-12-29 09:39

I\'m trying to get a currency rate with the Google Currency Calculator using the following jquery (dummy) code:

    $.getJSON(\"http://www.googl         


        
4条回答
  •  时光取名叫无心
    2020-12-29 10:08

    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.

提交回复
热议问题