Json eval is not able to parse data

I have a php program where I just test some sample data. I get an error ]after a list of items. How can I read this?

$dataDetailsList = array();
array_push($dataDetailsList, array('a' =>'1','b' =>'2','c' =>'3','d' =>'4','e' =>'5'));
echo json_encode(array("DataDetailsList"=>$dataDetailsList));

Then in my jQuery processor, I do like this:

function requestData() {
    $.ajax({
        url: 'live-server-data.php',
        success: function(data) {
            //alert(json);
            var jsonData = eval(" (" + data + ") ");
        },
        cache: false
    });
+3
source share
3 answers

Not using eval is evil. Use instead:

JSON.parse(data); // not supported in IE7 and below

I think you need to try

dataType: 'json'

I.e

$.ajax({
    url: 'live-server-data.php',
    dataType: 'json',
    success: function(data) {
        var jsonData = data;
        console.log(jsonData);
        $.each(jsonData.DataDetailsList, function(key, val) {
             var key = Object.keys(val)[0],
                 value = val[key];
             console.log(key); // output: a, b, c ...
             console.log(value); // output: 1, 2, 3,...
            // alternative
            for(var key in val) {
                console.log(key);
                console.log(val[key]);
            }
        });
    },
    cache: false
})
+3
source
function requestData() {
    $.ajax({
        url: 'live-server-data.php',
        success: function(data) {
            //alert(json);
            var jsonData = data;

        },
        cache: false,
        dataType: 'json' //data type that it will return 
    });
}
+4
source

You should just install dataTypein json, and jQuery will do the trick for you.

$.ajax({
    url: 'live-server-data.php',
    dataType: 'json',  //Added dataType json
    success: function(data) {
        //Now data is a javascript object (JSON)
    },
    cache: false
});
+2
source

All Articles