简体   繁体   English

$ .ajax调用Google API时返回错误

[英]$.ajax returns error when calling google api

I'm building an angular app where I have a 'public transportation' section and I want to get directions from the google api. 我正在构建一个有角度的应用程序,其中有一个“公共交通”部分,我想从Google API中获取路线。 I seem to get a valid url - when I enter the url in my browser, it returns the json. 我似乎得到了一个有效的网址-当我在浏览器中输入网址时,它会返回json。 But I get an error when user $.ajax. 但是当用户$ .ajax时出现错误。

Url 网址

http://maps.googleapis.com/maps/api/directions/json?
origin=Assenede,%20Belgi%C3%AB&destination=Industrieweg%20232,Gent-
Mariakerke,belgium&sensor=false&departure_time=1343641500&mode=transit

Function in angular controller 角度控制器中的功能

$scope.getDirections = function(){

        var directions_url = "http://maps.googleapis.com/maps/api/directions/json" +
         "?origin=" + $scope.details.formatted_address +
         "&destination=Industrieweg 232,Gent-Mariakerke,belgium" +
         "&sensor=false" + 
         "&departure_time=1343641500"+
         "&mode=transit";

         console.log(directions_url);

        $.ajax({
            type:"GET",
            dataType:"json",
            contentType:"application/jsonp",
            url: directions_url,
            success:function(data){
                alert(data);
            },
            error:function(xhr, status, error){
                console.log('error:' + status + error);
            }
        });
     }

Is there anyone that can come up with a solution? 有没有人能提出解决方案?

Thanks in advance. 提前致谢。 HS. HS。

The reason is that you don't set dataType properly as it's needed for using JSONP. 原因是您未正确设置dataType,这是使用JSONP所需的。 That's why you may get this error (I assume you're using jQuery): 这就是为什么您可能会收到此错误的原因(我假设您使用的是jQuery):

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 

But, even if you fix that by rewriting your code as follows: 但是,即使您通过重写代码来解决此问题,如下所示:

var url = "http://maps.googleapis.com/maps/api/directions/json?origin=Assenede,%20Belgi%C3%AB&destination=Industrieweg%20232,Gent-Mariakerke,belgium&sensor=false&departure_time=1343641500&mode=transit";

$.ajax({
    dataType:"jsonp",
    url: url,
    success:function(data){
        alert(data);
    },
    error:function(xhr, status, error){
        console.log('error:' + status + error);
    }
});

You'll get another error: 您会收到另一个错误:

Uncaught SyntaxError: Unexpected token : json:2
error:parsererrorError: jQuery110209881844320334494_1387726816235 was not called 

Which means that the remote side (Google in your case) sends you plain JSON instead of requested JSONP. 这意味着远程端(在您的情况下为Google)向您发送纯JSON而不是请求的JSONP。 As David mentioned, you need to revise your solution, and find another way of calling Google API (for instance, you can try to use Google Maps JavaScript API ). 正如David提到的,您需要修改解决方案,并找到另一种调用Google API的方法(例如,您可以尝试使用Google Maps JavaScript API )。

That URL returns plain JSON, but you will need JSONP to make a cross origin request. 该URL返回纯JSON,但是您将需要JSONP来进行跨源请求。 I'm not sure google has a JSONP option for maps, but you can look into some other ways around it like CORS or a server-side proxy. 我不确定Google是否为地图提供JSONP选项,但您可以采用其他方法来解决,例如CORS或服务器端代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM