简体   繁体   中英

Ajax - Can't get json data

I'm quite a beginner in ajax technology. I have a php for execute a mysql_query and I would like to use the result on the client side.

My database.php :

$q=mysql_query("SELECT name FROM customers");
$res=mysql_fetch_array($q);
echo json_encode($res);

and my client.php

<div id="output">this element will be accessed by jquery and this text replaced </div>

<script id="source" language="javascript" type="text/javascript">

$(function () 
{
$.ajax({                                      
  url: 'database.php',                  //the script to call to get data          
  data: "",                        

  dataType: 'json',                //data format      
  success: function(data)          //on recieve of reply
  {
    var name = data[0];              

     $('#output').html("<b>id: </b>"+name);

    } 
});
}); 

</script>

This is from some tutorial I've found. And as I saw the database.php works. It prints right data, but in the client.php I can't get anything. What could be the problem?

---------EDITED---------

So, seems like on the web server runs php 4.4.7 and looks like json_encode() function does not wokrs because of that. I' ve found a "solution". I include upgrade.php, which implemets new methods for older versions of php, as far as i understands. here is the webste of it http://include-once.org/p/upgradephp/

I can't upgrade php version so could this be a good solution? At the moment it does not works

First try to pring or alert your value.. if it is coming do according to your value u got..

 <div id="output">this element will be accessed by jquery and this text replaced </div>

    <script id="source" language="javascript" type="text/javascript">

    $(function () 
    {
    $.ajax({                                      
      url: 'database.php',                  //the script to call to get data          
      data: "",                        

      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
       alert(data);  // check data value is coming or not..
        var name = data[0];              

         $('#output').html("<b>id: </b>"+name);

        } 
    });
    }); 

    </script>

check its printing value for you or not...

$.ajax({                                      
  url: '/database.php',      //Correct path.                 
  //data: "",                       
  type : 'Get',
  dataType: 'json',                   
  success: function(data)          
  {
     alert(JSON.stringify(data)); 
     var name = data[0];    
     $('#output').html("<b>id: </b>"+name);
  } 
});
$.getJSON("database.php",function(data){
var entries = data;
$.each(entries,function(index,entry){
    //do stuff with json entry here
});
});

Properly be so:

database.php

if (isset($_POST['getData'])) {
    $q=mysql_query("SELECT name FROM customers");
    $res=mysql_fetch_array($q);
    echo json_encode($res);
    die();
}

client.php

<div id="output">this element will be accessed by jquery and this text replaced </div>

<script id="source" language="javascript" type="text/javascript">

$(function () 
{
    $.ajax({                                      
        url: 'database.php',                  //the script to call to get data          
        type: "post",
        data: { getData: true },
        dataType: 'json',                //data format      
        success: function(data)          //on recieve of reply
        {
            var name = data[0];              

            $('#output').html("<b>id: </b>"+name);

        } 
    });
}); 

</script>

Use this code in database.php

    $q=mysql_query("SELECT name FROM customers");

    while($res=mysql_fetch_array($q))
{
$a[]=$res['name'];
}
    echo json_encode($a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM