I am refactoring javascript and have a lot of similar POST calls to the same PHP url.
It would be nice to pass the postdata and callback function (defined in JiraChangeStatus), to a common function (SendPost).
I'm new to javascript, and was under the impression that it was possible to use a pointer / reference if the variable was the property of an object.
Have also tried using a variable for "xhr" instead of an object property, declaring "xhr" within JiraChangeStatus instead of SendPost, and even declaring it globally as a sanity check.
function JiraChangeStatus(index) {
var postdata = "&status="+document.getElementById("jiraT"+(index).toString()).value;
SendPost("changestatus.php",postdata, function(obj) {
alert("readyState: "+obj.xhr.readyState+"\r\nstatus: "+obj.xhr.status);
});
}
function SendPost(module,postdata,fn)
{
var obj = {xhr: new XMLHttpRequest()}
var issuekey = GetJson(document.getElementById("issue").value,'key');
obj.xhr.open("POST", "modules/jira/"+module, true);
obj.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
obj.xhr.onreadystatechange = fn(obj);
obj.xhr.send("username=user&issuekey="+issuekey+postdata);
}
When the callback function is executed I always see readystate 1 and status 0. I expect to see 4 and 200.
It appears javascript is passing a copy of the xhr object to the callback rather than the actual object.
These functions work when merged. Unless the properties of "xhr" are set within the scope of the callback function for "xhr", the callback doesn't get the value.
Please let me know what I'm doing wrong.