DOJO xhrGet如何使用返回的JSON对象?如何使用、对象、DOJO、xhrGet

2023-09-10 16:50:02 作者:野渡无人

我如何可以访问从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访问

JQuery中的 .getScript 和 .getJSON 方法的使用

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