0

我正在尝试从 ajax 登录系统(在科尔多瓦中)保存我的返回数据并在单独的页面中访问数据。它不起作用。重定向没有发生,返回数据不起作用!请请请帮忙!

索引.html

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi">
    <title>Hello World</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("form").submit(function () {

                var uname = document.getElementById("username").value;
                var pword = document.getElementById("password").value;
                var postData = {
                    username: uname,
                    password: pword
                };

                $.ajax({
                    url: "http://www.yellowcabsavannah.com/test.php",
                    type: "POST",
                    data: postData,
                    async: false,
                    dataType: 'json',
                    cache: false,
                    success: function (data) {

                        var ReturnMessage = data.message;

                        localStorage.setItem('message', ReturnMessage);

                        window.location = "map.html";
                    }
                });
                return false;
            });
        });
    </script>
</head>

<body>
    <form action="">
        <input type='text' id="username" name="username" placeholder="Username">
        <br>
        <input type='password' id="password" name="password" placeholder="password">
        <br>
        <input type="submit" id="submit" value="Login">
    </form>
</body>

测试.php

<?php
header('Content-Type: application/json');
$mysql_host = "";
$mysql_database = "";
$mysql_user = "";
$mysql_password = "";
$conn=mysql_connect($mysql_host,$mysql_user,$mysql_password);
mysql_select_db($mysql_database);

$username = mysql_real_escape_string($_POST['username']);
$password =  mysql_real_escape_string(MD5($_POST['password']));

$sql="SELECT * FROM accounts WHERE uname = '$username' AND pword = '$password'";

$r = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
    $name = $row["name"];
}

if(mysql_num_rows($r) != 1){
    echo json_encode(array("message" => "Nope! Wrong Login!"));
}
if(mysql_num_rows($r) == 1)
{
    echo json_encode(array("message" => $name));
}
?>

地图.html

<!DOCTYPE html>
<!--
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 -->
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>        <script type="text/javascript" src="cordova-2.3.0.js"></script>
        <script type="text/javascript">
            app.initialize();
            document.addEventListener()
            {
                ‘deviceready’, onDeviceReady, false);
            }
            function onDeviceReady()
            {
                $(document).ready(function () {
                    var RetMessage = localStorage.getItem('message');
                    alert(RetMessage);
                });
            }
            </script>

    </head>
    <body>
        <p>Your on map.html</p>
    </body>
</html>
4

1 回答 1

0

尝试

window.localStorage.setItem('message', ReturnMessage);

代替

localStorage.setItem('message', ReturnMessage);

$.mobile.changePage("map.html");

代替

window.location = "map.html";
于 2013-02-13T13:26:53.447 回答