XHR加载失败:GET作品有时,我想不通为什么

问题描述 投票:0回答:1

我试图提交给谷歌的地方和检索lat和长时间的请求。我使用的代码

let term = $("#getaddy").val();

$.ajax({
    url: "https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(term) +"&key=GOOGLEKEY",
    type: 'get',
    success: function(data) {
        if (data.status === 'OK') {
            // Get the lat/lng from the response
            let lat = data.results[0].geometry.location.lat;
            let lng = data.results[0].geometry.location.lng;
            console.log(lat);

        }
    },
    error: function(msg) {
        console.log("not sure what to ask for here to check issue");
    }
});

令人困惑的事情是,我会回来XHR加载失败或XHR完成(似乎是随机的)。虽然我尝试改变的事情,我不明白为什么它的工作原理或失败。每次我直接输入网址到它正确返回数据的浏览器,即使它说,它失败了。

有谁知道是什么原因造成的故障,或者我应该放什么的console.log以帮助确定问题?

谢谢

javascript ajax xmlhttprequest google-places-api google-places
1个回答
0
投票

我面临着同样的问题。我是有随机XHR加载失败或XHR完成。问题是触发功能按钮的定义。我不类型=“按钮”已定义的HTML按钮,这是越来越响应之前刷新页面。我添加类型的定义和它停止刷新页面,我的问题得到解决。

<button type="button"></button>

这是我的解决方案。我希望它为你工作了。

© www.soinside.com 2019 - 2024. All rights reserved.