在javascript中将数据发送到服务器后接收JSON对象

时间:2016-04-10 18:58:20

标签: javascript php json ajax

我正在尝试在登录时传递电子邮件和密码,然后点击使用PHP的数据库。一旦它测试了电子邮件和密码组合,php就会发回一个json对象。现在,我已经测试了php并且它正确运行并将显示具有正确“成功”或“失败”值的json对象,但要么我获得状态:请求被取消,或者“成功”不会被返回,但是“失败“确实得到了回报。我不知道这有什么问题。

这是我到目前为止的javascript:

//make sure the login is correct
function testLogin(email, password){
//make sure the boxes arent empty
if(email !== "" && password !=="")
{
    //create a json object, and 
    var jsonObject = {"email":email, "password":password};
    var finalObject = JSON.stringify(jsonObject);
    //alert(finalObject);

    //sending json object

        $.ajax({
            type: 'POST',
            url: 'WebPHP/check_login.php',
            data: finalObject,
            dataType: 'json',
            success: function(data)
            {

               if(data['result'] === "success"){
                   alet("good");
                    window.location.href = "AMessage.html";

            } else{
                    alert("Invalid Email or Password");
            }
            }
        });

}     return false;  

}

这是我到目前为止的php:

<?php

require 'db_connect.php';

header('Content-type: application/json');

$json = file_get_contents('php://input');

$jsondata = json_decode($json);

$email = $jsondata->{'email'};
$password = $jsondata->{'password'}
//$email = "TestUser";
//$password = "TestPasss";

$sql1 = " SELECT *
           FROM users
          WHERE email = '$email' AND password = '$password';";

$result = mysqli_query($Thesisdb, $sql1) or die(mysqli_error($Thesisdb));

$rows = $result->num_rows;

$post_data = array();

if($rows == 1){
    $post_data = array('result' => "success");
} else {
    $post_data = array('result' => "fail");
}

echo json_encode($post_data);

mysqli_close($Thesisdb);

0 个答案:

没有答案