-3
$('#button').click(function(){
    $.ajax({
        url : "test2.php",
        data : $("#tab"),
        type : "GET",
        success : function(b){
                      b = eval('('+ b +')');    
                      console.log((b['t']));
                      alert(b);
                  }
    });
});
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Ritu
  • 23
  • 6

4 Answers4

0

You shouldn't use eval(). You can either set the data type on the request as JSON or use JSON.parse();

$('#button').click(function(){
    $.ajax({
        url : "test2.php",
        data : $("#tab"),
        type : "GET",
        success : function(b){
                      b = JSON.parse(b);    
                      console.log((b['t']));
                      alert(b);
                  }
    });
});

//datatype as JSON

$('#button').click(function(){
    $.ajax({
        url : "test2.php",
        data : $("#tab"),
        type : "GET",
        dataType: "json",
        success : function(b){   
                      console.log((b['t']));
                      alert(b);
                  }
    });
});
kurt
  • 1,146
  • 1
  • 8
  • 18
0

You can get data using "parseJSON"

$('#button').click(function(){
    $.ajax({
        url : "test2.php",
        data : $("#tab"),
        type : "GET",
        success : function(b){
                 var obj = jQuery.parseJSON(b);
                 console.log(obj);

                }
    });
});
Jalpa
  • 697
  • 3
  • 13
0

Since you are using Jquery, try this:

$('#button').click(function(){
    $.ajax({
        url : "test2.php",
        data : $("#tab"),
        type : "GET",
        success : function(b){
            var obj=jQuery.parseJSON(b);
            alert(obj.<name>);
        }
    });
});
Manikiran
  • 2,618
  • 1
  • 23
  • 39
0

Since you are using AJAX, you can directly set the dataType as json and no need to again parse the data.

$('#button').click(function(){
$.ajax({
    url : "test2.php",
    data : $("#tab"),
    dataType : "json",
    type : "GET",
    success : function(b){
                  // b is in the JSON format, print the complete JSON
                  console.log(JSON.stringify(b));    
                  console.log(b['t']);
                  alert(b);
              }
  });
});
Chandan
  • 1,128
  • 9
  • 11