Welcome Guest, Not a member yet? Register   Sign In
jQuery ajax return
#1

[eluser]velti[/eluser]
hi, i know it’s not codeigniter, but i’m working on a scipt with the jQuery AJAX functionality:

i tried so much to get the “numFiles” through a return to use it in another function but i didn’t got it. is there a function in jquery to return the data correctly? i already searched in the jquery API but didn’t found something that could help me.
Code:
function numUploadedFiles()
{
    var numFiles = 0;
    $.post("'.base_url().'galleryajax/numUploadedFiles", { '.session_name().': "'.session_id().'"}, function(data){                    
        numFiles = parseInt (data);
        if(numFiles == 2) { /* only for check if the ajax request works */
            alert(numFiles);
        }
        $("#gal_uploadstatus").html(data);
        return numFiles;
    });
    return numFiles;
}

alert("Returned: " + numUploadedFiles());

The alert is "Returned: undefined"
#2

[eluser]Nick Husher[/eluser]
You're using your callbacks incorrectly. The Javascript won't pause to wait for a server response, it keeps executing procedurally unless told not to. In other words, that alert won't immediately recieve the data from the async call, so it'll return undefined.

Give the following code a try...
Code:
function numUploadedFiles()
{
    var numFiles = 0;
    $.post("'.base_url().'galleryajax/numUploadedFiles", { '.session_name().': "'.session_id().'"}, function(data){
       alert("AJAX Session called back. Asynchronous call executed successfully.");
       alert(data);
    });
}
#3

[eluser]velti[/eluser]
i thought so, therefore i already tried somethin nearly like your example.

but how can i get the return or use it in another function? in an oder ajax request function i could call the response text this way:

callbackfunction(rq,id)
{
alert(rq.responseText);
}

it think it should be also possible at jQuery?!?
#4

[eluser]Nick Husher[/eluser]
If the example code I gave you was returning what you were looking for, what you need to do is set up an event-driven interface for returning data. You can't just say, "Get me this data off the server" because that's not the way the system works. You ask for the data, and some interval later, it is delivered. You set a callback event to handle the new data and delegate the response to other parts of the system. If you're looking for a way of returning server data asynchronously, generally the workflow is this:

1. Fire off request and wait for callback
2. Set "loading" state in your view.
some amount of time passes
3. Callback function is executed:
4. Callback function parses response
5. Callback function unsets the "loading" state from your view and displays new data.

It's a little bit like a multithreaded application, in some sense.
#5

[eluser]velti[/eluser]
oh yes, i slightly remember Smile

i'll try something in the script next days and hope it will do it's applied work. i perhaps post my finished work here again.

thanks.




Theme © iAndrew 2016 - Forum software by © MyBB