first of all i’m a complete javascript and jQuery noob so i’m sorry if this question is dumb. but i really haven’t found the answer in the jQuery documentation or on google
i’m trying to perform an action on the response when the request is completed
but i can’t access responseText. i was under the impression the an jqxhr objext has a member responseText. but i get the following error ‘responseText is not defined’.
this.load = function(fileName, actionFunction){
var jqxhr = $.post(SERVER,{command : "load", filename : fileName})
.complete(function(){
actionFunction(responseText);
});
}
EDIT
if i do
.complete(function(responseText){ actionFunction(responseText);}
or what Dave Ward suggest
i don’t get an error but nothing happens
i know it isn’t the actionFunction() because if i input a string manually for testing purposes it does what i expect it to do.
when trying to see what parameter gets past on (prinintg it to the console in actionfunction) i get [object Object]; even when trying to convert it to a string (String(parameter))
END EDIT
what am i doing wrong or how do i access responseText in the .complete function ?
tnx in advance
The
$.postmethod is shorthand for a call to$.ajaxwith certain parameters predefined.From the documentation on the
ajaxfunction from jQuery’s website, thecompletecallback is passed two parameters:jqXHRandtextStatus, wherejqXHRis the actualXMLHTTPRequestobject.So it’s the second parameter you want to pass to your handler:
Note that, as Dave Ward says in his answer,
responseTextin this case is really just a status indicator, like “success” or “error” (hence jQuery’s name for it, “textStatus”). Given you have the word “response” in your variable name, I’m guessing (like he is) that you probably want thesuccesscallback instead ofcomplete: