简体   繁体   中英

Ajax MSSQL query using json and php returns undefined array

I have a problem when i alert( data[i].name). The result is undefined even if the array is returned. Anyone knows what the problem is?

This is My Javascript

    $('#plus').click(function(){
    $.ajax({
        type : 'POST',
        url : 'jqueryDBactionview.php',
        dataType : 'json',
        data: { },
        success: function( data){
            $.each( data, function(i, item) {
                alert( data[i].name);
            });
        }
    });
});

And this is my php

$query = "SELECT TOP (1) * ";
$query .= "FROM maillist WHERE bolag = 'FAL'";
$results = mssql_query($query);
$result = array();
while($row = mssql_fetch_array($results))
{
  $result[] = array(
          'id' => $row['id'],
          'company' => $row['company'],
          'name' => $row['name'],
          'mail' => $row['mail']
       );
}
echo json_encode($result);

请使用JSON.parse(data)转换您的json编码结果,然后在每个循环中使用它

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