jQuery 移动应用程序请求无效
jQuery requests on mobile application not working
我正在使用英特尔 XDK 为 Android 设备制作移动应用程序,我一直在使用模拟器和我的本地开发服务器 (127.0.0.1) 来测试我的 PHP 代码。我一直在使用以下方式联系我的服务器 $.ajax()
、$.post()
和 $.get()
。然后我决定我已经达到了一个合适的点,我应该构建应用程序 APK 文件,将 PHP 源推送到在线网站并通过适当的移动设备对其进行测试。所以我这样做了,我通过从 PMA 导出当前数据来创建数据库,然后更改所有请求中的所有 URL 以指向正确的位置,并将我的 PHP 源推送到 FTP。然后我测试了我的应用程序,并对结果感到非常震惊。
错误 #1:
PHP Fatal error: Can't use function return value in write context in
/home/scrifalr/public_html/sm/api/v1/modules/register/register.php on
line 9
我做了什么来修复:
我检查了源代码,显然在将 !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>
如您所见,它除了包含之外什么也没有。以下文件是 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;
});
}
所以问题二是,为什么我已经彻底测试并确保所有路径都正确并上传了完全相同的代码,为什么有些正在发送请求 e.g。登录,注册还有其他人(注销)不工作?
经过更多调试后发现 AJAX 调用失败,因为不允许同步 AJAX 调用。
我正在使用英特尔 XDK 为 Android 设备制作移动应用程序,我一直在使用模拟器和我的本地开发服务器 (127.0.0.1) 来测试我的 PHP 代码。我一直在使用以下方式联系我的服务器 $.ajax()
、$.post()
和 $.get()
。然后我决定我已经达到了一个合适的点,我应该构建应用程序 APK 文件,将 PHP 源推送到在线网站并通过适当的移动设备对其进行测试。所以我这样做了,我通过从 PMA 导出当前数据来创建数据库,然后更改所有请求中的所有 URL 以指向正确的位置,并将我的 PHP 源推送到 FTP。然后我测试了我的应用程序,并对结果感到非常震惊。
错误 #1:
PHP Fatal error: Can't use function return value in write context in /home/scrifalr/public_html/sm/api/v1/modules/register/register.php on line 9
我做了什么来修复:
我检查了源代码,显然在将 !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>
如您所见,它除了包含之外什么也没有。以下文件是 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;
});
}
所以问题二是,为什么我已经彻底测试并确保所有路径都正确并上传了完全相同的代码,为什么有些正在发送请求 e.g。登录,注册还有其他人(注销)不工作?
经过更多调试后发现 AJAX 调用失败,因为不允许同步 AJAX 调用。