Andy Lobel
Andy Lobel

Reputation: 3416

How to extract data from an ajax request?

I want to be able to get different array values in my ajax callback function, how would I do this? Im trying this atm..

function sendFeedback() {
$.post({
       url: 'send-feedback.php', 
       dataType: "json", 
       data: {
       user_id : this.bb_user, 
       star_count : this.limit, 
       feedback : document.feedback_form.feedback.value,
       set_anon : document.feedback_form.set_anon.checked
    }, success: function(output) {
    alert(output.coms[1]);
    // make it alert index 1 from the array??
}});
closeFeedback();
}

then in send-feedback.php

        $coms = array("value1", "value2");
        echo json_encode($coms);

i havent got a clue, thanks.

Upvotes: 1

Views: 715

Answers (1)

Teody C. Seguin
Teody C. Seguin

Reputation: 238

could you try this if it will work..

success: function(output) {
   var data = eval('(' + output + ')');
   alert(data[0]);
}

let me know..


did you made it like this right?

function sendFeedback() {
  $.post({
    url: 'send-feedback.php', 
    dataType: "json", 
    data: {
    user_id : this.bb_user, 
    star_count : this.limit, 
    feedback : document.feedback_form.feedback.value,
    set_anon : document.feedback_form.set_anon.checked
  },
  success: function(output) {
    var data = eval('(' + output + ')');
    alert(data[0]);
  }});

  closeFeedback();
}

Upvotes: 1

Related Questions