0

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
2
  • The question is : the returned JSON is undefined when trying to reach one JSON element, even though jquery parse function has been used !!!
    – Osahady
    Commented Oct 27, 2018 at 21:56
  • can you provide a screenshot of the response you are getting in the console?
    – Santu Roy
    Commented Oct 27, 2018 at 22:04

2 Answers 2

3

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.

1

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?
            } 
1
  • 3
    Simpler to just set dataType:'json' then no need to parse it, will be done internally and importantly any invalid json will fire error handler
    – charlietfl
    Commented Oct 27, 2018 at 22:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.