加载Google JS api时阻止了跨来源请求

Cross Origin Request Blocked while loading Google JS api

本文关键字:请求 Google JS api 加载      更新时间:2023-09-26

我遇到了一种前所未有的奇怪情况

我的代码目标:我正在使用Parse.com作为后台制作一个网络应用程序,并希望使用谷歌图表在网页中添加一些饼图。

我做了什么:正如文档中所述,我加载了GoogleJSapi,使用标签:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>

问题从这里开始。在我打开包含此标记的页面后,页面加载失败(从某种意义上说,它被卡在Read www.google.com...上,我只能看到一个白色屏幕)。我打开控制台,发现以下消息:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.parse.com/1/classes/MyClass. This can be fixed by moving the resource to the same domain or enabling CORS.

这里的MyClass是我在Parse后端创建的类的名称,在页面加载过程中会调用一个函数从中获取一些数据。

我尝试在一个单独的页面中加载JS api,它成功地工作了。有什么办法吗?在我的情况下,重要的是同时加载Parse.com的API和Google的JS API。有什么帮助吗?非常感谢。

附言:以下脚本是在页面加载过程中加载的:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.18.min.js"></script>
<script type="text/javascript" src="myquery.js"></script>
<script type="text/javascript" src="userquery.js"></script>

jquery.js是缩小的jQuery库。"myquery.js"和userquery.js是我自己编写的js文件。

您确定正在使用JSONP吗?我甚至在本地主机上也使用过谷歌的jsapi(尽管没有使用parse)。您可以将解析时的数据公开为web服务,然后可以对解析服务进行AJAX JSONP调用。

这对我来说很有效,没有任何问题,我在本地主机和各种域上都尝试过。

var jsonData = jQuery.ajax({
                        type: 'POST',
                        url: base_url+"index.php/<url>?callback=?",
                        dataType:"jsonp",
                        data: {userID : uID},
                        async: false
                        }).responseText;
var options = {
  title: 'Title',
  hAxis: {title: 'X'},
  vAxis: {title: 'Y'}
};
//create table for google visualization
var datatable = new google.visualization.DataTable();
//add rows and column to datatable
 ....
//draw chart
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data1, options);