移动应用程序上的jQuery请求不起作用

jQuery requests on mobile application not working

本文关键字:jQuery 请求 不起作用 程序上 应用 应用程序 移动      更新时间:2023-09-26

我正在使用适用于 Android 设备的英特尔 XDK 制作移动应用程序,我一直在使用模拟器和本地开发服务器 (127.0.0.1) 来测试我的 PHP 代码。我一直使用以下方式联系我的服务器$.ajax()$.post()$.get()。然后我决定我已经到达了一个合适的点,我应该构建应用程序APK文件,将PHP源代码推送到在线网站并通过适当的移动设备进行测试。所以我做到了,我通过从 PMA 导出当前数据来创建数据库,然后将所有请求中的所有 URL 更改为指向正确的位置,并将我的 PHP 源代码推送到 FTP。然后我测试了我的应用程序,并对结果感到非常震惊。

错误#1

PHP 致命错误:无法在写入上下文中使用函数返回值 /home/scrifalr/public_html/sm/api/v1/modules/register/register.php on 第 9 行

我做了什么来修复

我检查了源代码,显然将此!empty(trim($_POST['username']))更改为empty($_POST['username'])似乎修复了该错误。

所以问题一。为什么这个错误没有出现在我的本地服务器上,也没有告诉我我不能这样做?

错误#2

有一个登录/注册,可以发送所有有效的请求,我更改了上面的PHP,它们开始工作。但是我有一个注销页面似乎不起作用,代码如下所示:

注销.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>

如您所见,除了包含之外,它什么都没有。以下文件是 JavaScript 文件:

注销.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 调用。