Phonegap抛出“网络错误”;立即在手机上的PHP脚本的第一个字符-在Chrome/Dreamweaver中工作良好

Phonegap throws "Network Error" immediately on the first character of PHP script on phone - Works fine in Chrome/Dreamweaver

本文关键字:字符 第一个 脚本 Chrome 工作 Dreamweaver PHP 错误 网络 抛出 网络错误      更新时间:2023-09-26

我希望在一个相当棘手的问题上得到一点帮助。对于初学者,我将解释这个问题,因为它几乎看起来像是一个概念问题,然后我将发布一些代码示例来说明。

我使用HTML和JQuery开发了一个移动应用程序,后端使用mySQL和PHP。我正在使用phonegap将它连接到手机上。第一页(登录页)加载良好的手机,在chrome浏览器,并在DW实时视图。然而,在手机上,当我试图以任何方式与服务器交互(即单击登录按钮)时,我得到以下错误:

网络出现错误

SyntaxError: Unexpected token <</p>

现在,通过去掉第一个"<&quot;字符在我的PHP文件(即从"<php&quot;开始标记),错误变为如下所示:>

网络错误发生:

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
    });