PHP:代码:
$category_searchresult = $db->db_query("SELECT category_code, category_name, category_comment FROM qa_categories WHERE ".$search_by." LIKE '%".$search_string."%'");
$i=1;
$qa = array();
while($categories_query_result = mysql_fetch_array($category_searchresult))
{
$qa[$i][] = $categories_query_result['category_code'];
$qa[$i][] = $categories_query_result['category_name'];
$qa[$i][] = $categories_query_result['category_comment'];
$i++;
}
echo json_encode($qa);
JS代码:
$.ajax({
type: "POST",
url: "ajax_js.php",
data: search_data,
cache: false,
success: function(search_result) {
//Print Here
});
来自PHP的当前输出:
{"1":["4BA3CC","Fontaneria","Para la Casa"],"2":["CF0345","Herramientas","Herramients de Hogar"],"3":["1265CA","Luces","Luces de todo tipo"],"4":["4C4C9F","Vidrios","Reflectores de auto"]}
很多感谢
答案 0 :(得分:2)
添加
dataType: 'json'
到您的.ajax()调用。这将告诉jQuery将PHP中的JSON字符串解码回本机Javascript数据结构。或者,您可以在成功处理程序中使用data
参数,并自行显式解码:
success: function(data) {
var data = jquery.parseJSON(data);
}
答案 1 :(得分:1)
不确定输出应该是什么,但这是可以用来遍历json对象的代码:
for(var key in search_result) {
var result = search_result[key];
//result is an array, so you can traverse or access each value by the key
// or you can join all the values together
var joinedValues = result.join(' | ');
//joinedValues will be something like '4BA3CC|Fontaneria|Para la Case'
}
您可能希望将这些值设置为页面中的容器对象,以便显示它们。 希望这会有所帮助。