如何使用 ajax 和 jquery 从 php Web 服务返回简单文本


How to return simple text from php web service using ajax and jquery

我想使用php Web服务完成这个简单的登录任务。我只是试图根据我在 php 中回显的文本结果来验证用户名和密码。

.PHP:

<?php
    // Include confi.php
    include_once('confi.php');
    $found = false;
    $email = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : "";
    $password = isset($_POST['password']) ? mysql_real_escape_string($_POST['password']) : "";
    if(!empty($email) && !empty($password)){
        $login=mysql_num_rows(mysql_query("select * 
                                           from `login` 
                                           where `email`='$email' 
                                           and `password`='$password'"));
        $result =array();
        if($login!=0)
    {
    echo "success";
    }
else
{
echo "failed";
}
}
    @mysql_close($conn);
    /* Output header */
    header('Content-type: text/plain');

?>

如果用户名和密码匹配;则显示成功。

Jquery

 <script>
        $(function () {
            $("#logon").click(function () {
                var email = $("#username").val();
                var password = $("#pass").val();
                var dataString = "email=" + email + "&password=" + password;
                if ($.trim(email).length > 0 & $.trim(password).length > 0) {
                    $.ajax({
                        type: "POST",
                        url: "http://*****/login.php",
                        data:dataString,
                        crossDomain: true,
                        cache: false,
                        beforeSend: function () { $("#logon").html('Connecting...'); },
                        success: function (data) {
                            if (data == "success") {
                                alert(result+"You are in");
                                localStorage.login = "true";
                                localStorage.email = email;
                                window.location.href = "test.html";   
                            }
                            else if (data == "failed") {
                                alert("Login error");
                                $("#logon").html('Login');
                            }  
                        }
                    });
                }
            });
        });
    </script>

您缺少 JSON 函数,您必须对要发送回请求的内容进行编码

   <?php
    /*output the header here*/
     header("Content-Type: application/json");
            // Include confi.php
              include_once('confi.php');
              $response['Status'];//      declare a variable to store msg
              $found = false;
              $email = isset($_POST['email']) ? 
             mysql_real_escape_string($_POST['email']) : "";
                   $password = isset($_POST['password']) ? 
             mysql_real_escape_string($_POST['password']) : "";
              if(!empty($email) && !empty($password)){
           $login=mysql_num_rows(mysql_query("select * 
                                       from `login` 
                                       where `email`='$email' 
                                       and `password`='$password'"));
                $result =array();
               if($login!=0)
        {
           $response['Status']=""success";
        }
     else
        {
            $response['Status']="failed";
         }
       }
@mysql_close($conn);

在这里进行更改

 $result= json_encode($message);
  echo $result;

?>

在你 jQuery 数据到 AJAX 函数添加

            dataType:"json",
        success: function (data) {
               if (data.Status == "success") {    //here check the condition
                            alert(result+"You are in");
                            localStorage.login = "true";
                            localStorage.email = email;
                            window.location.href = "test.html";   
                        }
              else if (data.Status== "failed") { //here check the condition
                            alert("Login error");
                            $("#logon").html('Login');
                        }  
                    }