簡體   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