简体   繁体   English

jquery .ajax()方法失败

[英]jquery .ajax() method fail

Can anyone tell me what might be wrong with this javascript? 谁能告诉我这个javascript可能有什么问题?

        $.ajax({
            cache:false,
            url: 'http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false',
            success: function(data, textStatus, jqXHR) {
                console.log('success');
                console.log(data);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log('error');
                console.log(errorThrown);
                console.log(jqXHR);
            }
        });

The 'error' event handler function is invoked and this is my console output: 调用'error'事件处理函数,这是我的控制台输出:

error
undefined
XMLHttpRequest { mozResponseArrayBuffer=ArrayBuffer, status=0, more...}

I'm trying to learn how to make ajax calls with jquery, and I'm still pretty new to it. 我正在尝试学习如何使用jquery进行ajax调用,而且我还是很陌生。

I think you're doing a Cross domain request, which is blocked by your browser. 我认为您正在执行跨域请求,该请求已被浏览器阻止。

You could directly use the Maps API, which has convenient methods for such things. 您可以直接使用Maps API,它具有方便的方法。

Maps Documentation 地图文档

function codeAddress() {
  var address = document.getElementById("address").value;
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location
      });
    } else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  });
} 

I've just run this block of code through JSFiddle and get the same error problem. 我刚刚通过JSFiddle运行这段代码并得到了同样的错误问题。

however, when I go directly to the request URL I get this block of JSON: 但是,当我直接转到请求URL时,我得到了这个JSON块:

{
  "status": "REQUEST_DENIED",
  "results": [ ]
}

Are you getting the same ? 你有同感吗?

  • The first problem is that you can't make cross-domain Ajax requests without some extra work - you must use JSONP and not just 'plain' JSON or the server must allow the CDR via the appropriate access-control headers [ref] . 第一个问题是你不能在没有额外工作的情况下制作跨域Ajax请求 - 你必须使用JSONP而不仅仅是'普通'JSON,或者服务器必须通过适当的访问控制头[ref]来允许CDR。

  • However, the bigger problem here is that with v3 of the API, Google has taken away the ability to make JSONP requests (or at least it is undocumented at the moment). 但是,更大的问题是,使用API​​的v3,Google已经取消了发出JSONP请求的能力(或者至少目前没有记录)。 They would prefer you to use the Google Maps library geocoder instead: 他们希望您使用Google地图库地理编码器

     var geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'address': '1600+Amphitheatre+Parkway,+Mountain+View' }, function(data, status){ console.log(data); //data is an array of results }); 

For the moment, you could fall back to the v2 API if you absolutely must have Ajax geocoding but don't want to use the geocoder built into the Google Maps JS library: 目前,如果绝对必须使用Ajax地理编码但又不想使用Google Maps JS库中内置的地理编码器,则可以回退到v2 API

$.ajax({
    cache: false,

    url: 'http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=json&sensor=false&key=<your_key_here>',

    success: function(data, textStatus, jqXHR) {
        console.log('success');
        console.log(data); //geocoded data
    },

    error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus);
        console.log(errorThrown);
        console.log(jqXHR);
    },

    jsonp: 'callback',

    dataType: 'json'
});

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

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