【问题标题】:What does the Json onLoad do and how can i return something via the call?Json onLoad 做了什么,我如何通过调用返回一些东西?
【发布时间】:2011-09-28 00:34:52
【问题描述】:
function JsonCall() {
    JsonClient.setRequestHeader("Content-type", "application/json");
    JsonClient.send(Data);     
    JsonClient.onload = function() {    
      SomeOtherFunction(this.responseText);      
    }           
}

// This function is in someother class.

    var Object = {
        SomeOtherFunction: function() {
         return data;
        }
   }
  1. 我的 JsonCall 函数是什么 返回?
  2. 我能做些力所能及的事吗 从 onLoad 调用返回值 Json 对象?

我想在我的其他类中做一些逻辑并通过这个 JsonCall 函数返回值

【问题讨论】:

标签: javascript json


【解决方案1】:

我觉得很奇怪。 JsonClient 对象在这里应该是 XHR 对象。但是,XHR 对象提供了一个onreadystatechange 事件来通过检查readyState 属性来处理完整的传输。

唯一的例外是来自 Internet Explorer 的 XDomainRequest 对象。该对象确实具有.onload 属性。现在,您的JsonCall() 方法返回undefined。您需要添加 callback method 才能执行。喜欢

function JsonCall( SomeOtherFunction ) {
    JsonClient.setRequestHeader("Content-type", "application/json");
    JsonClient.send(Data);     
    JsonClient.onload = function() {    
      SomeOtherFunction(this.responseText);      
    }           
}

然后像这样称呼它

JsonCall( Object.SomeOtherFunction );

我希望 Object 在这里只是一个示例词。

【讨论】:

    【解决方案2】:

    Json onLoad 做了什么

    您为其分配一个功能。它在 HTTP 响应到达时运行该函数。

    我如何通过调用返回一些东西?

    你不能。这是事件驱动的编程。当一个事件发生时,你可以做一些事情来回应。在事件发生之前,您不能暂停代码的执行,然后继续执行。

    无论您需要对数据做什么,都必须在分配给 onLoad 的回调函数中完成。你不能把它传回去。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-04
      • 2010-10-01
      • 2011-08-17
      • 2018-08-09
      • 2015-03-09
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      相关资源
      最近更新 更多