2012-07-31 66 views
0

我使用以下代码来获取在控制台中打印的响应。但是我在控制台中显示为null。无法获取在控制台中打印的响应

在index.php网址我有JSON代码

new Ajax.Request('http://10.239.50.92/sample/index.php', { 
method:'get', 
    onSuccess: function(products){ 
       //alert(products.hasOwnProperty(value)); 
       //alert(products.hasOwnProperty()); 
       console.log(products.responseJSON); 
    } 
}); 

回答

0

也许这将帮助:

new Ajax.Request('http://10.239.50.92/sample/index.php', { 
    method:'get', 

    onSuccess: function (transport) { 
    var response = transport.responseText; 
    var json = eval('(' + response + ')') 
    console.log(json); 
    } 
}); 

或者试图改变在HTTP响应头,但似乎有一些困难在原型中有这个问题。

+0

嗨斯佩兰斯基,我有一个类似的问题,我似乎找出原因, 我在后端使用perl/cgi,我的代码如下 – 2012-09-07 20:45:44

+0

'new Ajax.Request('ajax-sr- update.cgi”,{ \t \t方法: '得到', \t \t的onSuccess:功能(传输){ \t \t \t变种响应= transport.responseText; \t \t变种JSON =的eval( '(' +响应+')') \t \t console.log(json); \t \t \t如果(JSON){ \t \t \t \t如果(json.error_message){ \t \t \t \t \t做一些事情; \t \t \t \t} else if(json。incident_id){ \t \t \t \t \t做某事别的; \t \t \t \t}否则{ \t \t \t \t \t显示的一般错误 \t \t \t \t} \t \t \t}否则{ \t \t \t \t一般错误 \t \t \t} \t \t}, \t \t onFailure处:功能(数据){ \t \t \t显示失​​败 \t \t} \t});' – 2012-09-07 20:50:21

0

只有当AJAX请求的响应类型是“application/json”时,才会返回responseJSON字段。

我会尝试以下方法:

console.log(transport) 

,看是否responseJSON属性是提供给您。

+0

dtsn,你是对的,我并没有在返回得到任何的JSON值,那都是纯文本。感谢这个想法。 – 2012-09-07 21:21:15