2015-11-21 26 views
-3
$('#button').click(function(){ 
    $.ajax({ 
     url : "test2.php", 
     data : $("#tab"), 
     type : "GET", 
     success : function(b){ 
         b = eval('('+ b +')');  
         console.log((b['t'])); 
         alert(b); 
        } 
    }); 
}); 
+0

b是一個json數組? – Ray

+0

http://api.jquery.com/jquery.get/ –

+0

你可以使用JSON.parse - 這裏的問題幾乎相同。 http://stackoverflow.com/questions/9991805/javascript-how-to-parse-json-array – Ray

回答

0

您不應該使用eval()。您可以將請求上的數據類型設置爲JSON或使用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); 
 
        } 
 
    }); 
 
});

+0

我們怎樣才能 – Ritu

0

由於您使用jQuery你可以使用 「parseJSON」

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

       } 
    }); 
}); 
0

數據,試試這個:

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

我在JSON – Ritu

+0

後,我想在阿賈克斯獲得 – Ritu

+0

下頁值使用值數據請告訴我適當的代碼我該怎麼做 – Ritu

0

由於您使用AJAX,你可以直接將dataType設置爲json,而不需要再次解析數據。

$('#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); 
       } 
    }); 
});