簡體   English   中英

移動應用程序上的jQuery請求不起作用

[英]jQuery requests on mobile application not working

我正在使用適用於Android設備的英特爾XDK制作移動應用程序,一直在使用模擬器和本地開發服務器(127.0.0.1)來測試PHP代碼。 我一直在使用以下方式與服務器聯系: $.ajax()$.post()$.get() 然后,我決定到達合適的位置,在其中構建應用程序APK文件,將PHP源代碼推送到在線網站上,並通過適當的移動設備對其進行測試。 因此,我做到了,我通過從PMA導出當前數據來建立數據庫,然后將所有請求中的所有URL都更改為指向正確的位置,並將PHP源代碼推送到FTP。 然后,我測試了我的應用程序,並對結果感到震驚。

錯誤#1

PHP致命錯誤:在第9行的/home/scrifalr/public_html/sm/api/v1/modules/register/register.php的寫上下文中無法使用函數返回值

我做了什么來解決

我檢查了源,顯然在將!empty(trim($_POST['username']))更改為empty($_POST['username'])似乎可以修復該錯誤。

所以問題一 為什么此錯誤沒有出現在我的本地服務器上,然后又不告訴我我不能這樣做?

錯誤2

我有一個登錄/注冊,可以發送所有有效的請求,我更改了上述PHP,它們開始起作用。 但是我有一個注銷頁面,該頁面似乎不起作用,代碼如下所示:

logout.html

<!DOCTYPE html>
<html>

<head>
    <link type="text/css" rel="stylesheet" href="css/styles.css">
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="intelxdk.js"></script>
    <script src="cordova.js"></script>
    <script src="xhr.js"></script>
    <script src="js/libs/app.js"></script>
    <script src="js/libs/functions.js"></script>
    <script src="js/logout.js"></script>
    <title>Logout</title>
</head>

<body>

</body>

</html>

如您所見,除了includes,它什么也沒有。 以下文件是JavaScript文件:

logout.js

window.onload = function () {
    getSessionData();
    $.post(
        "http://sm.script47.net/api/v1/modules/logout/logout.php", {
            "userID": window.userID
        }, function (data) {
            alert(data);
            if (data == 1) {
                document.location.href = "index.html";
            } else {
                showTimedMessage(data, "error", 4000);
            }
        }
    );
};

如您所見,它包含了post請求和一個名為getSessionData()的函數,因此在經歷了一段嘗試調試的時代之后,我得出的結論是getSessionData()失敗了,再次看起來很奇怪在仿真器中工作,並且所請求文件的所有路徑均正確。 只適合那些希望看到該功能的人:

window.token = null;
window.userID = null;
window.username = null;
window.emailAddress = null;
window.IP = null;

function getSessionData() {
    $.ajax({
        url: 'http://sm.script47.net/api/v1/modules/sessionData/sessionData.php',
        "type": "GET",
        dataType: "json",
        async: false // This comment is not in the code, I know I shouldn't have this I'm using it for now and it works with it like this.
    }).done(function (data) {
        window.token = data.token;
        window.userID = data.userID;
        window.username = data.username;
        window.emailAddress = data.emailAddress;
    }); 
}

因此,第二個問題是,即使我進行了徹底的測試並確保所有路徑正確並上傳了完全相同的代碼,結果又如何?為什么有些請求(例如登錄,注冊而其他(注銷))無法發送呢?

經過更多調試后,發現AJAX調用失敗,因為不允許同步AJAX調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM