繁体   English   中英

Javascript 无法在移动设备 (Android) 上运行,但在台式机上运行良好

[英]Javascript not working on mobile (Android) but fine on desktop

我搜索了档案,但没有找到任何特定于我的查询的内容。

在 JavaScript 中,我有一个带有回调函数的函数,该函数向邮政编码 API 发出请求以获取邮政编码的坐标。

const getPostCodeLatLng = (strPostCode, callback) => {
    alert("used API"); // !!! DOESN'T ALERT ON MOBILE !!!
    const request = new XMLHttpRequest();
    request.addEventListener('readystatechange', () => {
        if(request.readyState == 4){
            const jsnPostCode=JSON.parse(request.responseText);
            callback(jsnPostCode);}});
    request.open('GET', 'http://api.getthedata.com/postcode/' + strPostCode);
    request.send();
};

getPostCodeLatLng(strInputFieldValue, (jsnPostCode) => { // use the function to get data about the postcode and callback to this function
            if(jsnPostCode.status=="match"){
                alert("used API"); // !!! DOESN'T ALERT ON MOBILE !!!
                let strPostCodeLatLng = "LatLng(" + jsnPostCode.data.latitude + ", "
                + jsnPostCode.data.longitude + ")";
                setFieldswithLatLng(strPostCodeLatLng);
                objDisplayFindCons.value=`Postcode: ${strInputFieldValue}`;}
            else objDisplayFindCons.value=`No match found for Postcode: ${strInputFieldValue}`;})

这些功能在台式机上运行良好,但在三星手机或平板电脑上都不起作用。 我在所有设备上都使用 Chrome。

第二部分代码是较大部分的一部分,该部分响应将数据输入文本框中的事件,验证为可能的邮政编码(使用正则表达式),然后根据第一个函数进行请求。 然后解析并检查 JSON 文本响应以查看是否找到匹配项(服务器为未找到的邮政编码返回有效的 JSON)。
我很清楚它一切正常,直到它遇到函数调用getPostCodeLatLng() ,它从未在移动设备上运行任何一个alert("used API")语句。

我是 JavaScript 新手,发现编码回调函数和事件具有挑战性,但我看不到任何明显的错误/原因在移动设备上失败。

我正在做的事情是否存在已知问题或限制?

有没有办法解决这个问题或在移动设备上有效地调试它?

请帮忙!

谢谢,

菲尔

所以我尝试了各种方法,发现问题出在使用http请求上。

显然,从现在开始,来自 Android 上 Chrome 浏览器的所有请求都需要是https

所以改变request.open('GET', 'http://api.getthedata.com/postcode/' + strPostCode); request.open('GET', 'https://api.getthedata.com/postcode/' + strPostCode); 直接解决了问题。

这是一篇提到更改的文章:-
https://www.thesslstore.com/blog/https-will-now-be-the-default-for-all-android-p-apps/

你生活和学习...

暂无
暂无

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

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