只是试图在视图中显示我通过ajax从控制器获取的数组,但它显示的是[object Object],[object Object]而不是array。请在下面检查我的js文件:
$(".faq_title").click(function(){ var title = $(this).text(); $.post('faq/get_faq_data', { title: title }, function (data) { document.write(data); }, "json"); });
使用JSON.stringify转换JSON object到JSON string。
JSON.stringify
JSON object
JSON string
试试这个:
$(".faq_title").click(function(){ var title = $(this).text(); $.post('faq/get_faq_data', { title: title }, function (data) { document.write(JSON.stringify(data)); }, "json"); });
我认为,这对您有帮助-
$(".faq_title").click(function(){ var title = $(this).text(); $.post('faq/get_faq_data', { title: title }, function (data) { //document.write(JSON.stringify(data)); var my_obj = data; $.each(my_obj, function (i, z) { var id = my_obj[i].faq_id; var title = my_obj[i].faq_title; var question = my_obj[i].faq_question; var answer = my_obj[i].faq_answer; console.log("ID: " + id + "; Title: " + title + "; Question: " + question + "; answer: " + answer); }); }, "json"); });