0

I'm writing a ColdFusion application that fills with some HTML content some divs once the corresponding button is clicked.

What happens is that the readyState never goes up from the initial state of 1. The fact that makes me crazy is that I used the same AJAX code in other modules that work fine.

I tried manually the code in my applet "___AJAX_load_translator.cfm" to see if works correctly (inputting a complete url with parameters and query string) and it works.

I put many alerts in these javascript functions to trace if the url was created correctly, the parameters were formatted correctly and so on. Everything seems fine. This is driving me crazy. The result is the same on FireFox and IE.

function getHTTPObject(){
    if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
    else 
    if (window.XMLHttpRequest) return new XMLHttpRequest();
    else {
        alert("No AJAX support.");
        return null;
    }
}

function setOutput(divID){
    if(httpObject.readyState == 4 && httpObject.status == 200){
        document.getElementById(divID).innerHTML = httpObject.responseText;
    } //  else alert(httpObject.readyState + '   ' + httpObject.status);
}

function loadeditor(divID,CP,PP){
    <CFOUTPUT>var CF_TOKENS = "CFID=#CFID#&CFTOKEN=#CFTOKEN#";</CFOUTPUT>
    var operativeurl= "___AJAX_load_translator.cfm?"+CF_TOKENS+"&CP="+CP+"&PP="+PP;
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("POST", operativeurl, true);
        httpObject.onreadystatechange = setOutput(divID);
        httpObject.send(null);
    }
}

I noticed that, putting an alert into the setOutput function, it displays a sudden readystate of 1. Then the browser statusbar shows the status of wait for a call to the server, that disappears quite immediately. It seems that the call is really done in that moment, and probably it is imho.

But it seems to me that after that readyness of the call (state 1) there is no more proceeding. It seems somehow blocked. Or, the function setOutput is deactivated. Maybe a second change to a state of 4 happens and this state is not registered by the callback ? In this case, why the DIV is not updated with the new content ?

Thanks for any help.

1 Answers1

3
    httpObject.onreadystatechange = setOutput(divID);
                                             ^^^^^^^

You're calling/executing your setouput function right then and there, and whatever the function returns becomes on the onreadystatechange callback "pointer".

Remove the (divID) portion, so you assign the function itself, not whatever it returns:

    httpObject.onreadystatechange = setOutput;
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • OK. I think I have got the point. But the problem is that I should pass to setOuput the pointer of the DIV that must be filled with the new content. What if I use a global variable ? – Sergio Bonfiglio Jul 27 '15 at 18:30
  • http://stackoverflow.com/questions/8582663/pass-arguments-into-ajax-onreadystatechange-callback – Marc B Jul 27 '15 at 18:32
  • THANK YOU FOR YOUR PRECIOUS HELP. – Sergio Bonfiglio Jul 27 '15 at 18:32
  • don't use globals. if you issue two or more of those calls close together, you'll have absolutely NO guarantee as to which one returns first, and you'll very likely use the WRONG value in that global. – Marc B Jul 27 '15 at 18:33
  • Please allow me another question, that is related to the previous one. After an operation of assign of a new innerHTML content, does occur any modification into the DIV DOM structure ? I mean, if I try to restore the previous content with another innerHTML assignment, nothing happens. Why ? – Sergio Bonfiglio Jul 28 '15 at 17:42