1

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");
});
NiceTry
  • 360
  • 5
  • 7
  • 16

2 Answers2

3

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");
    });
Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
  • great, now it shows the array in the following format: [{"faq_id":"1","faq_title":"title 1","faq_question":"question 1","faq_answer":"answer 1"},{"faq_id":"2","faq_title":"title 1","faq_question":"question 2","faq_answer":"answer 2"}] How can I grab the value "answer 1" and print only this value for example? – NiceTry Jun 07 '14 at 12:45
  • @TProDeveloper: Please see updated answer. ( think, the would help you). But please let me know if there are some problem in my code. :) – Ishan Jain Jun 07 '14 at 13:03
  • the code is perfect but it prints the result with document.write() only. I don't know why console.log() doesn't work. Also I would like to pass the variables **question** and **answer** to the view. Is it possible? – NiceTry Jun 07 '14 at 14:32
  • @TProDeveloper: "console.log()" - It's not a jQuery feature but a feature of Firebug (or some other tool). IF you are using Chrome than you can see result in Chrome’s Web Inspector >> “Console” tab. And for passing variable - You could set up a hidden field in your form and put variable value into hidden field. after it you can directly access the hidden field into your code. (you must see this "Pass javascript variable to Controller in MVC from View - http://stackoverflow.com/questions/22167263/how-to-pass-javascript-variable-to-controller-in-mvc-from-view") – Ishan Jain Jun 08 '14 at 11:01
2

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

E.g.

 document.write(JSON.stringify(data));
Bootstrapper
  • 1,089
  • 3
  • 14
  • 33
  • great, now it shows the array in the following format: [{"faq_id":"1","faq_title":"title 1","faq_question":"question 1","faq_answer":"answer 1"},{"faq_id":"2","faq_title":"title 1","faq_question":"question 2","faq_answer":"answer 2"}] How can I grab the value "answer 1" and print only this value for example? – NiceTry Jun 07 '14 at 12:37