jquery shows [object object] instead of array [duplicate]

Just trying to show in the view the array which I got from controller via ajax but it shows [object Object],[object Object] instead of array. Please check my js file below:

$(".faq_title").click(function(){

    var title = $(this).text();

    $.post('faq/get_faq_data', { title: title }, function (data) {

       document.write(data);
        }, "json");
});

Answers:

Answer

Use JSON.stringify to convert JSON object to JSON string.

Try this:

$(".faq_title").click(function(){
    var title = $(this).text();
    $.post('faq/get_faq_data', { title: title }, function (data) {
       document.write(JSON.stringify(data));
    }, "json");
});

I think, the would help you -

$(".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");
    });
Answer

You need to expand the object with JSON.stringify().

E.g.

 document.write(JSON.stringify(data));

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.