本文介绍了DOJO xhrGet如何使用返回的JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以访问从xhrGet返回以外的数据中得到自己?萤火虫表明,JSON对象已调用结果的数组,它存储的响应JSON对象,但是当我尝试访问它是空。所以:我怎么访问的最后code线接收到的数据

  VAR JSON = dojo.xhrGet({
    网址:'/ disease_web / graphMlDownload / getEdgeInformation /',handleAs:JSON,内容:{EdgeID的:EdgeID的,graphname:this._canvas.path},
    负载:函数(响应){
        返回响应;
    }
});
执行console.log(json.ioArgs);
执行console.log(json.results);
 

解决方案

在默认情况下dojo.xhrGet异步调用,因此执行console.log(json.results)为空,因为它只是后dojo.xhrGet运行,但响应到来之前从服务器。

  VAR xhrGet = dojo.xhrGet({
        网址:/ some_rul
        handleAs:JSON,
        处理:函数(响应){
            console.info(2,'响应',响应);
            console.info(3,'xhrGet.results [0]',xhrGet.results [0]);
        }
 });
 console.info(1,xhrGet.hasOwnProperty(结果));
 

结果是:

1个假

2的响应 - ['从服务器的一些数据']

3 xhrGet.results [0] - 同样的数据在回应通过xhrGet访问

How can I access the data returned from the xhrGet outside of the get itself? Firebug shows that the "json" object has an array called results, which stores the json Object from the response, but when I try to access it it is null. So: how do I access the received data on the last code line?

var json = dojo.xhrGet({
    url :'/disease_web/graphMlDownload/getEdgeInformation/', handleAs:"json",content : {  edgeid : edgeId, graphname:this._canvas.path},
    load:function(response){
        return response;
    }
});
console.log(json.ioArgs);
console.log(json.results);
解决方案

By default dojo.xhrGet is called asynchronously, so console.log(json.results) is null because it's run just after dojo.xhrGet, but before response comes from server.

var xhrGet = dojo.xhrGet({
        url: "/some_rul",
        handleAs: "json",
        handle: function(response) {
            console.info(2,'response',response);                
            console.info(3,'xhrGet.results[0]',xhrGet.results[0]);
        }
 });
 console.info(1,xhrGet.hasOwnProperty('results')); 

The result is:

1 false

2 response - ['some data from server']

3 xhrGet.results[0] - same data as in 'response' accessed via xhrGet

这篇关于DOJO xhrGet如何使用返回的JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 23:36