Osahady
Osahady

Reputation: 449

Why JQuery AJAX call is returning an undefined object?

I have made a jquery ajax call to a php server script but I have encountered an error message telling me that the data returned of type JSON is not defined. . . So, how to solve this problem? Here is jquery ajax call:

    $.ajax({
                url: 'listWorkProcess.php',
                method: 'post',
                data: data,
                success: function(feedback){
                    $.parseJSON('feedback');
                    console.log(feedback);// loged correctly
                    alert(feedback.message);//here is the undefined message
                    $('#table').html(feedback.row_html);//this not executed Why?
                }   
            });//end of ajax

Upvotes: 0

Views: 3614

Answers (2)

Kishan Sorathiya
Kishan Sorathiya

Reputation: 111

Try this AJAX code:

In your code small change needed this change is include " dataType = 'json' " in AJAX code like below

$.ajax({
    url: 'listWorkProcess.php',
    dataType:"json",
    method: 'post',
    data: data,
    success: function(feedback){
        console.log(feedback);// loged correctly
        alert(feedback.message);//here is the undefined message
        $('#table').html(feedback.row_html);//this not executed Why?
    }   
});

This is simple method to got response which is set in controller in form of jsonencode formate just give dataType:'json' to AJAX code.

Upvotes: 3

D.V.D.
D.V.D.

Reputation: 257

You are passing just a string to the parseJSON. You need to assign it to something and use the data you get like :

            success: function(feedback){
                var data = $.parseJSON(feedback);
                console.log(data);// loged correctly
                alert(data.message);//here is the undefined message
                $('#table').html(data.row_html);//this not executed Why?
            } 

Upvotes: 1

Related Questions