jQuery getJSON在跨域上不起作用 [英] jQuery getJSON won't work on cross domain

查看:148
本文介绍了jQuery getJSON在跨域上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google地图的REST api将输入的地址转换为坐标。为此,我使用jQuery的getJSON函数,因为它使用本机JSONP调用。



尽管它不起作用,但它并没有取得成功,所以警报从未被调用过。



点击(功能(){
straat = $('#straat')。val();
stad = $($#$) $('#Stad')。val();
land = $('#Country')。val();
if(straat ==|| stad ==|| );
alert(Onvoldoende gegevens!Vul straat,stad en land aan voor het gebruik van de'bereken coordinaten' functie。);
} else {
$ .getJSON(http://maps.google.com/maps/api/geocode/json?callback=?,
{
地址:straat +,+ stad +,+土地,
传感器:false
},
函数(数据){
alert(data);
});
}
});


解决方案

Google地图地理编码API不支持JSON-P ;它会忽略你的回调参数,因此跨域JSON-P将不起作用。



你应该使用改为官方Google Maps Javascript库。在内部,库使用JSON-P传递信息(因为它几乎是进行跨域请求的唯一方式),但该URL仅供官方库使用。


I'm trying to use google maps' REST api to convert an typed in adress to coordinates. To do so I use jQuery's getJSON function as it uses a native JSONP call.

Though it won't work, it just doesn't get to success, so the alert is never called.

$("#makeCords").click(function(){
    straat = $('#straat').val();
    stad = $('#Stad').val();
    land = $('#Country').val();
    if(straat == "" || stad == "" || land == ""){
        alert("Onvoldoende gegevens! Vul straat, stad en land aan voor het gebruik van de ´bereken coordinaten´ functie.");
    }else{
        $.getJSON("http://maps.google.com/maps/api/geocode/json?callback=?",
        {
            adress: straat +", " + stad +", " + land,
            sensor: "false"
        },
          function(data) {
              alert(data);
          });
    }
});

解决方案

The Google Maps Geocoding API does not support JSON-P; it will ignore your callback parameter, thus cross-domain JSON-P won't work.

You should use official Google Maps Javascript library instead. Internally the library does use JSON-P to pass the information (since it's almost the only way to do cross-domain requests), but that URL is reserved to official library use only.

这篇关于jQuery getJSON在跨域上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆