Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSON响应中的回调函数_Javascript_Json - Fatal编程技术网

Javascript JSON响应中的回调函数

Javascript JSON响应中的回调函数,javascript,json,Javascript,Json,在下面的代码中,我进行了JSON调用。如果我得到响应,我应该调用带有响应的sendData函数 var Model= function () { function GetData() { // Sending the request and i am getting the response. JsonClientScheduleCareProvider.onload = function () {

在下面的代码中,我进行了JSON调用。如果我得到响应,我应该调用带有响应的
sendData
函数

var Model= function () {

        function GetData() {    
            // Sending the request and i am getting the response. 

            JsonClientScheduleCareProvider.onload = function () {
                return this.responseText;
            };
            // error handling
            JsonClientScheduleCareProvider.onerror = function (e) {

            };
        return {
            GetApps: GetData,
        }

   }();    

我面临的问题是在
jsonData
得到响应之前,调用
SendData
。我只需要在收到响应时执行SendData函数

您需要等待,直到收到您的回复。为此,请使用
回调
函数

试着这样做:

    var jsonData = Model.GetApps();
    if (!jsonData) {
        Ti.API.warn("JsonData");
        SendData(jsonData);
    }

这和OP似乎缺少一个
}
var Model= function () {

    function GetData( callback ) {    
        // Sending the request and i am getting the response. 

        JsonClientScheduleCareProvider.onload = function () {
            callback( this.responseText );
        };
        // error handling
        JsonClientScheduleCareProvider.onerror = function (e) {
            callback( null );
        };
    }
    return {
        GetApps: GetData,
    }

}(); 

Model.GetApps( function(jsonData){
   if (!jsonData) {
    Ti.API.warn("JsonData");
    SendData(jsonData);
   }
} );