0

I have AJAX request and want to do my code after i get response. For it i try callback function:

AJAX

        function some_function(callback) {
        var mydateArray=[];
        Ext.Ajax.request({
            method: "POST",
            url: url_servlet+"/dateIntensityJson.jsp",
            success: function(response){
                jsonObject = JSON.parse(response.responseText);
                mydateArray = jsonObject.data;              
            }
        });
        callback(mydateArray);
    }

My function:

        function intWinCreate(dateArray){
            ...some code...
            }

calling functions:

 some_function(intWinCreate(dateArray));

And i get error:

ReferenceError: dateArray is not defined

I do callback wrong? And i DONT USE jQuery.

Kliver Max
  • 5,107
  • 22
  • 95
  • 148

2 Answers2

3

Move your callback call inside the callback you pass to the Ajax method, so that it's executed after the ajax response is received :

 Ext.Ajax.request({
        method: "POST",
        url: url_servlet+"/dateIntensityJson.jsp",
        success: function(response){
            jsonObject = JSON.parse(response.responseText);
            mydateArray = jsonObject.data;              
            callback(mydateArray);
        }
    });

Right now, you executing callback(mydateArray) before the success function is executed.

As was noticed by Mattias, you also don't seem to pass a function as callback but the result of a function call. You probably want to call some_function like this :

some_function(intWinCreate);
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 2
    Also, the call should be `some_function(intWinCreate)` instead of `some_function(intWinCreate(dateArray))`. You *pass* the function, you don't *call* it (and pass the *result*). – Mattias Buelens Jun 16 '13 at 15:32
  • @dystroy: i put callback inside success but not help. Now try your comment. Im call function like in my question becouse function intCreateWin ~60 code line. – Kliver Max Jun 16 '13 at 15:34
  • I don't know Ext but are you sure you need to parse the response ? – Denys Séguret Jun 16 '13 at 15:36
  • Now i get error: `TypeError: b is null`. What you mean about parse response? I parse it to get json array. – Kliver Max Jun 16 '13 at 15:37
  • 1
    @KliverMax There's no `b` in the code you showed us. – Denys Séguret Jun 16 '13 at 15:37
  • @dystroy: yeah. But i dont have variable with this name. – Kliver Max Jun 16 '13 at 15:38
  • Maybe i put my code somewhere? In pastebin or something. – Kliver Max Jun 16 '13 at 15:40
  • @KliverMax Looks like something went wrong in Ext then, probably a bad call to a library method. Try digging through the stack trace of that exception until you find some of your own code. You might also want to (temporarily) use a development version of Ext to get a better understanding of *what* has become `null`, since the minified version obfuscates the variable names. – Mattias Buelens Jun 16 '13 at 15:41
3

You seem to be misunderstanding what AJAX is. Let me remind you what the first A letter stands for: Asynchronous. This means that the request is immediately sent and the Ext.Ajax.request function returns immediately. The result of this AJAX call might come much later. So you cannot possibly expect to utilize this mydateArray immediately after you called your AJAX function. The only safe place where you can utilize the results of an AJAX call is inside the callback itself:

Ext.Ajax.request({
    method: "POST",
    url: url_servlet+"/dateIntensityJson.jsp",
    success: function(response){
        jsonObject = JSON.parse(response.responseText);
        callback(jsonObject);
    }
});

As you can see from this example we are calling the callback inside the AJAX success callback because it is only inside this function that the result of your AJAX call will be available.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928