0

下面是ajax代码。
问题在评论中

 $.ajax({
            type: "POST",
            url: "dbtryout2_2.php",
            data:datastr,
            dataType: "json",
            success: function(arrayphp)
            {
                              //ARRAYPHP is a json array and iam displaying the
                              //contents of the array one by one as link  
              $.each(arrayphp, function(i, element) {
              var link = $('<a href="#" class="album"><font color="red">' + element + '</font></a>');
              var image='<input type="image" src="img4.jpg" height="30px" width="30px">';

                 $(".searchby .searchlist").append(image).append(link); 
                });

            }
         });

上面的代码不起作用。我没有正确使用 json 数组。下面是 php 代码/ PHP 代码/

if(!strcmp($_REQUEST['id'],'a2'))
{
 $lang=$_REQUEST['lang'];$type=$_REQUEST['type'];
 $query="select distinct(cat_name) from song where lang='$lang' and category='$type'"; 
 $result=mysql_query($query,$con) or die("Query failed".mysql_error());

 $arrayphp=array();
while($row=mysql_fetch_array($result))
{
    $element=$row['cat_name'];
        array_push($arrayphp,$element);
}
    //here is the json array that is being sent to the above html code
  echo json_encode($arrayphp);
} 
4

1 回答 1

0

您需要将从 PHP 传递的字符串转换为适当的 JSON 对象/数组,然后才能将其用作 javascript 对象/数组。

success: function(arrayphp) {
    arrayphp = jQuery.parseJSON(arrayphp);  // will make proper json from a json string
                          //ARRAYPHP is a json array and iam displaying the
                          //contents of the array one by one as link  
    $.each(arrayphp, function(i, element) {

    ....
于 2013-08-28T15:40:52.787 回答