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

如何使用ajax和jquery从PHPWeb服务返回简单文本,php,jquery,mysql,ajax,post,Php,Jquery,Mysql,Ajax,Post,我想使用PHPWeb服务完成这个简单的登录任务。我只是试图验证用户名和密码的基础上的文本结果,我在我的php回声 PHP: 您缺少json函数,您必须对要发送回请求的内容进行编码 ?> 在您的jquery中,将数据添加到ajax函数中 打印返回的字符串(如alert(数据))时的输出是什么?无输出。现在还不是晚上,还没到那个地步/您在html中缺少数据类型ajax@devpro–数据类型是可选的。如果省略它,jQuery将只使用Content-Type响应头。您有crossDomain:tru

我想使用PHPWeb服务完成这个简单的登录任务。我只是试图验证用户名和密码的基础上的文本结果,我在我的php回声

PHP:


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

?>

在您的jquery中,将数据添加到ajax函数中


打印返回的字符串(如
alert(数据))时的输出是什么?无输出。现在还不是晚上,还没到那个地步/您在html中缺少数据类型ajax@devpro–
数据类型是可选的。如果省略它,jQuery将只使用Content-Type响应头。您有
crossDomain:true、
(可能与您认为的不同)和一个绝对URI。您是否提出跨产地申请?您看过浏览器开发工具中的控制台了吗?难道您没有一条错误消息抱怨缺少Access Control Allow Origin标头吗?问题中的PHP说它正在发回纯文本。JavaScript中没有任何内容表明它需要JSON。jQuery将纯文本解析为纯文本,这很好。在这种情况下,返回什么并不重要,返回html或json都可以实现相同的功能
<?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');


?>
 <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>
   <?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;
            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');
                        }  
                    }