2017-06-02 148 views
0

对象后,我有这样的AJAX jQuery代码:访问AJAX功能

$("#serial_number").submit(function(e) { 

    var url = "/getVoucher"; // the script where you handle the form input. 

    $.ajax({ 
     type: "GET", 
     url: url, 
     data: $("#serial_number").serialize(), 
       dataType: "json", 
     success: function(data) 
     { 
     console.log(data); 

     } 
    }); 

    e.preventDefault(); // avoid to execute the actual submit of the form. 
}); 

,我得到这个弗朗的console.log: enter image description here

我怎样才能访问对象代码元素?

我尝试使用:data.Code也尝试data.Description但浏览器给我错误...如何访问Ajax代码中的成功函数内?

+2

看起来像是双重嵌套,如果你仍然在控制台日志中看到'data',试试'data.data.Code'? –

+0

是的,那是一个错误...非常感谢 –

回答

2

当你console.log一个变量,在控制台输出是它的内容,所以如果你仍然看到:

Object{ data: Object } 

这意味着变量data有它内部的一个关键data。请尝试:

console.log(data.data.Code); 

访问对象的内容。