Json_encode 在 php 中并在 jquery 中检索

Json_encode in php and retrieve it in jquery

我是 Jquery 的新手,所以我发布了这个问题,可能很简单。

我在php页写了查询,我想在jquery页检索它。php页如下。

   $chkuser = "SELECT A.* FROM mcd_users A WHERE A.User_Handle=('{$User_Handle}') 
   AND A.User_Password=('{$User_Password}')  AND A.Rec_Status='A' AND      
   A.User_Status='A'";

    try {
        $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $dbh->prepare($chkuser);  
        //$stmt->bindParam("id", $_GET[id]);
        $stmt->execute();
        $login = $stmt->fetchObject();
        $dbh = null;

        echo '{"item":'. json_encode($login) .'}'; 

JQuery 代码:-

      $.ajax({
                        url: 'ajax_files/login.php',
                   crossDomain: true,
                            type: 'post',
                        data: $("#loginForm").serialize(), 
                       success: function(data){
                        if(data!== null)
                        {alert(data);
                            //var res=$json.decode(data);
                            //alert(res);                           
                        }

                    }
        }); // Ajax Call

现在当我 alert(data); 它显示。

  {"item": 
{"User_Id":"1110","Rec_Status":"A","Rec_Seq":"3","UserApplication_Id":"101",
"User_Type":"U","
User_Handle":"MCD_Admin","User_Password":"827ccb0eea8a706c4c34a16891f84e7b",
"PasswordChanged_Date":"2014-08-04",
"User_Status":"A","User_Email":"gasian@muj.com","User_Phone":"8877665544",
"Locked":"N","Reset":"N","Customer_Id":"10","CreatedBy":"1110",
"CreatedOn":"2013-07-30  00:00:00","ModifiedBy":"1110","ModifiedTime":"2014-08-04 10:49:20"}}   **

现在我只需要在 jquery.js page.How 中显示 User_Handle 即可检索 it.Any 帮助。

jquery.parsejson

你是这里的朋友你可以在这里阅读更多关于它的信息http://api.jquery.com/jquery.parsejson/

关于如何使用它的示例代码

var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" );

试试这个

alert(data.item.User_Handle)

编辑

如果上面的代码不起作用试试这个

var result = JSON.parse(data);
alert(result.item.User_Handle);

jquery 的 parseJSON 可以。

var a=$.parseJSON('{"item": {"User_Id":"1110","Rec_Status":"A","Rec_Seq":"3","UserApplication_Id":"101","User_Type":"U"," User_Handle":"MCD_Admin","User_Password":"827ccb0eea8a706c4c34a16891f84e7b","PasswordChanged_Date":"2014-08- 04","User_Status":"A","User_Email":"gasian@muj.com","User_Phone":"8877665544","Locked":"N","Reset":"N","Customer_Id":"10","CreatedBy":"1110","CreatedOn":"2013-07-30 00:00:00","ModifiedBy":"1110","ModifiedTime":"2014-08-04 10:49:20"}}');

alert(a.item.User_Handle);
success: function(data) {
        var json = $.parseJSON(data);
        alert(json.item.User_Handle);
       }

将 ajax 请求中的数据类型 json 设置为:-

     $.ajax({
        url: 'ajax_files/login.php',
        crossDomain: true,
        type: 'post',
        dataType: "json",
        data: $("#loginForm").serialize(), 
        success: function(data){
            if(data!== null)
            {
                alert(data.item.User_Handle);                      
            }
        }
  }); 

并得到 User_Handle 作为:- alert(data.item.User_Handle)