I have a problem with returning data back to the function I want to return to. Code below:
function ioServer(request_data, callback) { $.ajax({ cache: false, data: "request=" + request_data, dataType: "json", error: function(XMLHttpRequest, textStatus, errorThrown){}, success: function(response_data, textStatus){ callback(response_data); }, timeout: 5000, type: "POST", url: "/portal/index.php/async" }); } function processRequest(command, item, properties) { var request = {}; request.command = command; request.item = item; request.properties = properties; var toServer = JSON.stringify(request); var id = ioServer(toServer, processResponse); return id; } function processResponse(fromServer) { if (fromServer.response == 1) { return fromServer.id; } }
I call this part of the code by calling the processRequest function inside another function. Sending a request and receiving a response work very well. However, I need to return the "id" value from the response back to the processRequest function so that it, in turn, can return this value to its caller. As far as I can follow, the return in processResponse goes back to $ .ajax, but I need it to go back to processRequest.
The BTW if statement in processResponse refers to the value set by the server-side PHP script to indicate whether the request is allowed (for example, if the user has not been registered, fromServer.response will be 0). It has nothing to do with the success / error procedures of the $ .ajax object.
Thank you for help.
@Jani: Thanks for the answer, but could you clarify a little more? A function that requires an 'id' has the following code:
$(
You want to say that I should try to execute this part of the code in the processResponse function? Because this is not what I wanted to do; these functions are intended for a universal solution to support the state server part. That is why I did not post this piece of code.
javascript jquery ajax callback return
Ryan
source share