使用Ajax和服务器MySQL在Phonegap中开发登录页面


Developing the login page in Phonegap using Ajax and on server MySQL

我已经被这个问题困扰了好几天了。我使用Ajax组的web开发技术从服务器调用php文件。似乎没有调用成功方法。这是我的代码:

function handleLogin() {
var form = $("#loginForm");    
//disable the button so we can't resubmit while we wait
//$("#submitButton",form).attr("disabled","disabled");
var e = $("#email", form).val();
var p = $("#password", form).val();
console.log("click");
if(e != "" && p != "") {
    //var str = form.serialize();
    //alert(str);
    $.ajax({ 
                     type: 'POST', 
                     url: 'http://prefoparty.com/login.php', 
                     crossDomain: true,
                     data:  {email: e, password :p},
                     dataType: 'json', 
                     async: false,
                     success: function (response){ 
                        alert ("response"); 
                        if (response.success) { 
                            alert("you're logged in");
                            window.localStorage["email"] = e;
                            window.localStorage["password"] = md5(p); 
                            //window.localStorage["UID"] = data.uid;           
                            window.location.replace(main.html);
                        } 
                        else {
                            alert("Your login failed");
                            //window.location("main.html");
                        }

                     },
                     error: function(error){
                         //alert(response.success);
                        alert('Could not connect to the database' + error);
                        window.location = "main.html";
                    }
    }); 
}
else {
    //if the email and password is empty
    alert("You must enter email and password");
}
return false;
}

在php中,我使用了一个典型的MySQL调用,当我从Google Chrome浏览器运行这个文件时。它正确地返回了JSON。这是我的php:

            <?php
            require_once('includes/configinc.php');
            $link = mysql_connect(DB_HOSTNAME, DB_USERNAME,DB_PASSWORD) or die("Could not connect to host.");
            mysql_select_db(DB_DATABASE, $link) or die("Could not find database.");
            $uname = $_POST['email'];
            $password = $_POST['password'];

            $sql = "SELECT * FROM User_Profile WHERE Email = '$uname' AND Password = 'md5($password)'";
            $result=mysql_query($sql);
            $num_row = mysql_num_rows($sql);
            $row=mysql_fetch_array($result);

            if (is_object($result) && $result->num_rows == 1) {
            $response['success'] = true;
            }
            else
            {
            $response['success'] = false;
            }
            echo json_encode($response);
             //echo 'OK';
            ?>

请检查我的代码并指出我做错的地方。

提前感谢大家:)

添加

 header("access-control-allow-origin: *") 

到您的PHP页面顶部将解决您访问跨域请求的问题