Ionic App无法提交获取请求

时间:2014-08-28 21:15:09

标签: ajax angularjs ionic-framework

我一直在努力向谷歌提交简单的get请求api

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=xxxx

我一直收到错误: XMLHttpRequest无法加载https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8 ... ius = 500& types = food& name = cruise& key = xxxx。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许原点'8100'进入。

我在网上看了这个,它与CORS有关,但我还没有弄明白。如果有人能指出我正确的方向,那就太好了。

感谢

2 个答案:

答案 0 :(得分:0)

我能够使用离子调用该URL。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" />
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <link href="components/lib/ionic/css/ionic.css" rel="stylesheet" />
    <script src="components/lib/ionic/js/ionic.bundle.js"></script>
    <script src="components/lib/ionic/js/angular/angular-resource.js"></script>
    <script type="text/javascript">
        angular.module('myApp',
[
 'ionic',
 'ngResource'
])
.factory('GetData', function ($resource)
{
    return {
        getg: function ()
        {
            return $resource('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=xxxx', null).get();
        }
    }
})
.controller('SomeController', function (GetData)
{
    GetData.getg().$promise.then(
        function (answer)
        {
            console.log(answer);
        });
});
    </script>
</head>
    <body ng-app="myApp" ng-controller="SomeController"></body>
</html>

答案 1 :(得分:0)

对我来说有用的是安装这个插件:

  • 科尔多瓦-插件白名单
  

控制台:

     

cordova插件添加cordova-plugin-whitelist