Search in This Blog

TypeError e is undefined in jquery json

Problem:

jQuery is good JavaScript library. It provide lot of facilities,one of famous facility is it's Ajax. but some time you can get a error message "TypeError e is undefined" or "TypeError: e is undefined" in jQuery Ajax error "TypeError: e is undefined" specially when working with Ajax and JSON.
Normally it occurs when you trying to access a variable without  defined or declare in JavaScript. But when you declare all the variable and error still coming then how to remove it?

Look below code:

In file1.php file

----------------------------------------------------------------------
$c_id=$_GET['c_id'];
$s_id=$_GET['s_id'];
// get result from Database
$rs=$util->get_all_code($c_id, $s_id) ;
$rows = array();
while ($row = $rs->fetch_array(MYSQLI_BOTH)) {
     $rows[]=$row;
}
print '{"All_codes":'. json_encode($rows).'}';
----------------------------------------------------------------------
Keep not that "All_codes" is the top heading array in the above JSON result. And you will use that "All_codes" in jQuery result fetch loop as below code:

 In file2.php file

----------------------------------------------------------------------
 $.ajax({
                type: 'GET', async: false, cache: false, timeout:5000, url: 'ajax_util.php',
                data: { c_id: 1,s_id:2 },
                dataType: 'json',
                success: function (data) {
                                                         $.each(data.All_code, function(idx, obj) {
                                                            var nm=obj.nm;
                                                         });
                                                       }
});
Keep in mind that you have used "All_code" instead of "All_codes"
----------------------------------------------------------------------

Solution:

It's solution is re-again check the variable name in JSON result in PHP and jQuery.

Example:

In file1.php file

----------------------------------------------------------------------
$c_id=$_GET['c_id'];
$s_id=$_GET['s_id'];
// get result from Database
$rs=$util->get_all_code($c_id, $s_id) ;
$rows = array();
while ($row = $rs->fetch_array(MYSQLI_BOTH)) {
     $rows[]=$row;
}
print '{"All_codes":'. json_encode($rows).'}';
----------------------------------------------------------------------

 In file2.php file

 $.ajax({
                type: 'GET', async: false, cache: false, timeout:5000, url: 'file1.php',
                data: { c_id: 1,s_id:2 },
                dataType: 'json',
                success: function (data) {
                                                         $.each(data.All_codes, function(idx, obj) {
                                                            var nm=obj.nm;
                                                         });
                                                       }
});
Keep in mind that you have used "All_codes"



Previous
Next Post »