user1667633
user1667633

Reputation: 4619

PHP & jQuery parsing the data

I am getting the jsong response from php backend like

  feedback =   {"html":"<form action='someurlonrest' method='post' style='margin-left:100px;' ><div style='padding: 10px;'>Ticket No<input type='text' id='tkt' name='tkt' ><\/div><div style='padding: 10px;'>somthing <input type='text' id='smthing' name='smthing' ><\/div><div style='padding: 10px;'>Query about <input type='text' id='query' name='query' ><\/div><input type='submit' class='classname' ><\/form>"}

why i am getting undefined message whenever i am trying to do

alert(feedback.html);

Updated

function get_fb_success(){

    var feedback = $.ajax({
        type: "POST",
        url: "feedback.php",
        async: false
    }).success(function(){
        setTimeout(function(){get_fb_success();}, 100000000000000000);
    }).responseText;
    alert(feedback);
    //$('#log_success').val('');


    $('#log_success').html(feedback.html);
    //$('div.feedback-box-success').html('success feedback');
}

Upvotes: 0

Views: 93

Answers (2)

jeffery_the_wind
jeffery_the_wind

Reputation: 18178

The answer to your question is that you can see the object value you are looking for. See the fiddle here: http://jsfiddle.net/aWC8G/

Below that, your ajax function looks a little strange. I think it should look like this:

function get_fb_success(){

    var feedback;
    $.ajax({
        type: "POST",
        url: "feedback.php",
        async: false,
        dataType: 'json',
        success:function(data){
            feedback = data;  //assuming you are 
        }
    });
    alert(feedback);
    //$('#log_success').val('');


    $('#log_success').html(feedback.html);
    //$('div.feedback-box-success').html('success feedback');
}

Of course is is not an asynchronous call to the server, if you want real async ajax, do it like this (which should work since your function is not actually returning any value:

function get_fb_success(){

    $.ajax({
        type: "POST",
        url: "feedback.php",
        dataType: 'json',
        success:function(feedback){
            $('#log_success').html(feedback.html);
        }
    });

}

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270607

Two things:

Inform jQuery you expect JSON back, by adding a dataType: 'json' property, then don't forget to pass the response into the success callback.

At the time you are doing $('#log_success').html(), the data isn't actually available yet since it is an asynchronous call (perhaps this is why you're trying to use that long setTimeout()?). Do it in the success handler instead:

function get_fb_success(){

    // feedback is a jqXHR object, *not* the JSON response!
    var feedback = $.ajax({
        type: "POST",
        url: "feedback.php",
        async: false,
        // Specify datatype
        dataType: 'json',
        success: function(response) {
           // Call .html() in the success() handler
           $('#log_success').html(response.html);
        }
    });
}

Upvotes: 1

Related Questions