簡體   English   中英

使用JavaScript將數據發送到服務器后接收JSON對象

[英]Receiving JSON object after sending data to server in javascript

我試圖將登錄單擊時的電子郵件和密碼傳遞給使用PHP的數據庫。 一旦測試了電子郵件和密碼組合,PHP就會發回一個json對象。 現在,我已經測試了php,它可以正常運行,並且將顯示具有正確“ success”或“ fail”值的json對象,但我的狀態是:請求已取消,或者“ success”從未返回,而是“失敗”。 我不知道這是怎么回事。

這是到目前為止的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);

您的成功警報有一個錯字,在您的示例中,您使用alet alert

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM