Note that there are some explanatory texts on larger screens.

plurals
  1. POReusing my AJAX post function by using CallBacks
    text
    copied!<p>I have an AJAX post function I am using for my communication between my server &amp; the users webpage. But I have a problem with dealing with the response from the server in my resuable function.</p> <p>If you see the following example you will see that the function outputEmailDetailsResult() is called &amp; run on the line "post( "forgotPass.py", emailParam, outputEmailDetailsResult() );", ie, when the function is passed as a param. But it should only run inside the post function in reation to the server response:</p> <pre><code>var xmlhttp; function post( dest, params, callbackFunction ) { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { callbackFunction; } } xmlhttp.open("POST",dest,true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send( params ); // "fname=Henry&amp;lname=Ford"); } function emailLoginDetails() { var emailParam = "email=sam"; // + document.getElementById( "forgotEmail" ).innerText; alert( emailParam ); post( "forgotPass.py", emailParam, outputEmailDetailsResult() ); } function outputEmailDetailsResult() { try { document.getElementById( "statusOutput" ).value = xmlhttp.responseText; } catch ( ex ) { document.getElementById( "statusOutput" ).value = "Failed to get response from server"; } } </code></pre> <p>I am new to passing functions as a parameters(I think they're called callbacks?) &amp; I am unsure I am doing it correctly?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload