Phonegap抛出“网络错误”;立即在手机上的PHP脚本的第一个字符-在Chrome/Dreamweaver中工作良好
Phonegap throws "Network Error" immediately on the first character of PHP script on phone - Works fine in Chrome/Dreamweaver
我希望在一个相当棘手的问题上得到一点帮助。对于初学者,我将解释这个问题,因为它几乎看起来像是一个概念问题,然后我将发布一些代码示例来说明。
我使用HTML和JQuery开发了一个移动应用程序,后端使用mySQL和PHP。我正在使用phonegap将它连接到手机上。第一页(登录页)加载良好的手机,在chrome浏览器,并在DW实时视图。然而,在手机上,当我试图以任何方式与服务器交互(即单击登录按钮)时,我得到以下错误:
网络出现错误
SyntaxError: Unexpected token <</p>
现在,通过去掉第一个"<"字符在我的PHP文件(即从"<php"开始标记),错误变为如下所示:>
网络错误发生:
SyntaxError: Unexpected token ?
由于脚本的第一个字符现在是"?",这证明脚本的第一个字符是有问题的。这清楚地表明phonegap在我的PHP实现中不能很好地发挥作用,我不知道为什么。就像我之前提到的,当在Google Chrome浏览器或DW Live视图中查看时,该应用程序可以完美地工作。
我正在使用JQuery "$.ajax()"函数向服务器发送AJAX请求,代码如下:首先,查看移动端JQuery的第一页(login):
<div data-role="page" id="login_page">
<div data-role="header">
<h1>Login Page2</h1>
</div>
<div data-role="content">
<form id="login_form">
<div data-role="fieldcontain">
<label for="login_email">Email:</label>
<input type="email" name="login_email" id="login_email" value="" />
<label for="login_pass">Password:</label>
<input type="password" name="login_pass" id="login_pass" value="" />
<h4 id="login_notification"><?php echo 'Notifications will appear here...'; ?></h4>
<button data-theme="b" id="login_submit" type="button">Login</button>
</div>
</form>
</div>
<div data-role="footer">
<center>
<div data-role="controlgroup" data-type="horizontal">
<a href="#reg_page" data-role="button">Don't have an account? Register here.</a>
</div>
</center>
</div>
</div>
现在,相关的Javascript/JQuery:
// Login page handler
$(document).on('pageshow', '#login_page', function() {
$("#login_notification").text("page loaded");
// Check to see if a user account is loaded in, if not create one set to dummy variables
if (localStorage.getItem("logged_user") === null) {
var userInfo = {
"user_id": -1,
"logged_in": false,
};
localStorage.setItem("logged_user", JSON.stringify(userInfo));
}
$(document).on('click', '#login_submit', function(){
$("#login_notification").text("button clicked");
var formData = $("#login_form").serialize();
$.ajax({
type: "POST", // Method of sending data to server
url: "php_scripts/login_handle.php", // php script being sent to
cache: false, // requested pages won't be cached by server
data: formData, // data to be sent to server
dataType: "json", // data type to be received back from server
success: onLoginSuccess, // function to call on success
error: onError // function to call on error
});
return false;
//alert('Testing alert!');
});
});
最后,PHP脚本,我不认为它本身是问题(就像我说的,我怀疑脚本的第一个字符,不管它是什么,是抛出错误):
<?php
include_once 'db_connect.php';
$valid_login = false;
$error_msg = '';
// Check to see if necessary fields are set
if (isset($_POST['login_email'], $_POST['login_pass'])) {
// Extract data
$email = $_POST['login_email'];
$raw_pass = $_POST['login_pass'];
// Check the database to see if the user exists
$check_stmt = $mysqli->prepare("SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1");
if ($check_stmt) {
if ($check_stmt->bind_param('s', $email)) {
if ($check_stmt->execute()) {
$check_stmt->store_result();
// Get variables from result
$check_stmt->bind_result($user_id, $username, $db_password, $salt);
$check_stmt->fetch();
// Append the salt to the raw password and hash them together
$password = hash('sha512', $raw_pass . $salt);
// Check if user w/ email address exists
if ($check_stmt->num_rows == 1) {
// Check to see if passwords match
if ($password == $db_password) {
// If they do, set login boolean to true
$valid_login = true;
}else {
$error_msg .= 'Invalid email or password';
}
}else {
$error_msg .= 'No user with that email address. ';
}
}else {
$error_msg .= 'Database error (execute, prepare). ';
}
}else {
$error_msg .= 'Database error (bind, prepare). ';
}
}else {
$error_msg .= 'Database error (check, prepare). ';
}
}else {
$error_msg .= 'Please fill out the required fields. ';
}
$output = array('valid' => $valid_login, 'error_msg' => $error_msg, 'user_id' => $user_id);
echo json_encode($output);
?>
拜托,任何有过phonegap经验的人,请帮我弄清楚为什么它不工作!谢谢你!
您必须将ajax 'url'更改为驻留在web服务器上的php脚本的绝对url。在phonegap应用程序中没有php脚本
$.ajax({
type: "POST", // Method of sending data to server
url: "http://domain.com/php_scripts/login_handle.php", // php script being sent to
cache: false, // requested pages won't be cached by server
data: formData, // data to be sent to server
dataType: "json", // data type to be received back from server
success: onLoginSuccess, // function to call on success
error: onError // function to call on error
});
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- Regex-从字符第N次出现到字符串末尾的匹配
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- 在跨度中每3个字符添加一个空格
- 如何在JavaScript中每隔第n个字符分割一个字符串
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 看起来第一个console.log是'It’不太对
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- 节点发布错误对象的第一个“属性”
- Javascript:表单验证getElementById仅返回第一个id元素
- 删除最后一个无效字符,而不是第一个(javascript验证)
- 获得第一个n个文本字符的网页与他们的风格等