douweilei2307
2014-04-03 07:01
浏览 31
已采纳

无法从php中调用Ajax调用中的json数据

I'm able to convert my data into JSON format in php and while sending that data to Ajax call, I'm not able to get the details. In fact first the length of Json data shows 87, where it is actually 2.

My php code is

// credentials of MySql database.
$username = "root";
$password = "admin";
$hostname = "localhost"; 

$data = array();
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");

$selected = mysql_select_db("Angular",$dbhandle)
or die("Could not select Angular");
//execute the SQL query and return records
$result = mysql_query("SELECT id,name,password FROM User");

//fetch tha data from the database  
while ($row = mysql_fetch_array($result)) {

    $id = $row{'id'};
    $name = $row{'name'};
    $password = $row{'password'};
    $data[] = array('id' => $id, 'name' => $name, 'password' => $password);
}
echo json_encode($data);

Output it shows is

[
    {
        "id": "1",
        "name": "Rafael",
        "password": "rafael"
    },
    {
        "id": "2",
        "name": "Nadal",
        "password": "nadal"
    }
]

My Ajax call is

$.ajax({
    type: "GET",
    url: "ListUsers.php",
    success: function (dataCheck) {
        console.log(dataCheck.length);
        for(index in dataCheck) {
            /*
            console.log("Id:"+dataCheck[index].id);
                            console.log("Name:"+dataCheck[index].name);
                            console.log("Password:"+dataCheck[index].password);*/

        }
    },
    error: function () {
        alert("Error");
    }
 });

Please let me know if there is any thing wrong in my code

图片转代码服务由CSDN问答提供 功能建议

我能够在php中将我的数据转换为JSON格式,并在将数据发送到Ajax调用时,我' 我无法得到细节。 事实上,首先Json数据的长度显示为87,实际上是2.

我的PHP代码是

  //凭证 MySql数据库。
 $ username =“root”; 
 $ password =“admin”; 
 $ hostname =“localhost”;  
 
 $ data = array(); 
 //连接到数据库
 $ dbhandle = mysql_connect($ hostname,$ username,$ password)
或者死掉(“无法连接到MySQL”); \  n 
 $ selected = mysql_select_db(“Angular”,$ dbhandle)
ot die(“无法选择Angular”); 
 //执行SQL查询并返回记录
 $ result = mysql_query(“SELECT id,name  ,密码FROM User“); 
 
 //从数据库中获取数据
while($ row = mysql_fetch_array($ result)){
 
 $ id = $ row {'id'}; 
 $  name = $ row {'name'}; 
 $ password = $ row {'password'}; 
 $ data [] = array('id'=> $ id,'name'=> $ name,  'password'=> $ password); 
} 
echo json_encode($ data); 
   
 
 

显示的输出是
  [
 {
“id”:“1”,
“name”:“Rafael”,
“password”:“rafael”
},
 {
“id  “:”2“,
”名称“:”Nadal“,
”密码“:”nadal“
} 
] 
   
 
 

我的Ajax 调用是
  $ .ajax({
 type:“GET”,
 url:“ListUsers.php”,
 succe  ss:function(dataCheck){
 console.log(dataCheck.length); 
 for(index in dataCheck){
 / * 
 console.log(“Id:”+ dataCheck [index] .id);  
 console.log(“名称:”+ dataCheck [index] .name); 
 console.log(“密码:”+ dataCheck [index] .password); * / 
 
} 
},\  n错误:function(){
 alert(“Error”); 
} 
}); 
   
 
 

如果有任何事情,请告诉我 我的代码错误

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douvcpx6526 2014-04-03 07:07
    已采纳

    set the dataType to 'JSON' and you are all set:

    $.ajax({
      type: "GET",
      url: "ListUsers.php",
      success: function (dataCheck) {
        /* ... */
      },
      error: function () {
        alert("Error");
      },
      dataType: 'JSON'
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doushichun9409 2014-04-03 07:03

    The dataCheck inside success() is a string. You must convert it like this:

    var data = $.parseJSON(dataCheck);
    

    Now you can use it in you for loop like

    data.forEach(function(item){
    
    console.log(item.name)
    
    });
    
    评论
    解决 无用
    打赏 举报
  • doujiaci7976 2014-04-03 07:20

    This should be your Ajax call:

    $.ajax({
        type: "GET",
        url: "ListUsers.php",
        success: function (dataCheck) {
               var data = $.parseJSON(dataCheck);
               $(data).each(function(item){
                        console.log(item.name);
               });       
        },
        error: function () {
            alert("Error");
        }
     });
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题