code_junkie

onreadystatechange function not being called

Nov 13th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script>
  4. function recordScore(str)
  5. {
  6.  
  7. if (str.length==0)
  8. {
  9.  
  10. return;
  11. }
  12.  
  13. xmlHttp=GetXmlHttpObject();
  14. if (xmlHttp==null)
  15. {
  16. alert ("Your browser does not support AJAX!");
  17. return;
  18. }
  19.  
  20. var url="http://hellworld3.appspot.com/findcountry";
  21. var params = "screenname="+document.getElementById("screenname1").value+"&score="+document.getElementById("score1").value;
  22. alert("params: "+params);
  23. xmlHttp.open("POST",url,true);
  24.  
  25. xmlHttp.onreadystatechange = function()
  26. {
  27. alert("entered call back function. readstate value is: "+xmlHttpreadyState+". Response Text is: "+xmlHttp.responseText);
  28.  
  29. if (xmlHttp.readyState==4)
  30. {
  31. document.getElementById("message").innerHTML=xmlHttp.responseText;
  32. }
  33. }
  34. xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  35. xmlHttp.send(params);
  36.  
  37. //Testing code for synchronous mode
  38. //alert("Http get status is: "+xmlHttp.status);
  39. //alert("Http ready state value is: "+xmlHttp.readyState);
  40. //alert("Http get response text is: "+xmlHttp.responseText);
  41. //document.getElementById("message").innerHTML=xmlHttp.responseText;
  42. }
  43.  
  44.  
  45. function GetXmlHttpObject()
  46. {
  47. var xmlHttp=null;
  48. try
  49. {
  50. // Firefox, Opera 8.0+, Safari
  51. xmlHttp=new XMLHttpRequest();
  52. }
  53. catch (e)
  54. {
  55. // Internet Explorer
  56. try
  57. {
  58. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  59. }
  60. catch (e)
  61. {
  62. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  63. }
  64. }
  65. return xmlHttp;
  66. }
  67.  
  68. </script>
  69.  
  70.  
  71. </head>
  72.  
  73.  
  74. <body>
  75.  
  76. <form name="testform">
  77. Screename:
  78. <input type="text" id="screenname1" name="screenname">
  79. <br/>
  80. Score:
  81. <input type="text" id="score1" name="score" onchange="recordScore(this.value)">
  82. <br/>
  83. <p id="message">test</p>
  84.  
  85. <input type="submit" value="Submit">
  86. </form>
  87.  
  88. </body>
  89.  
  90.  
  91. </html>
  92.  
  93. alert("entered call back function. readstate value is: "+xmlHttpreadyState+". Response Text is: "+xmlHttp.responseText);
  94.  
  95. xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  96. xmlHttp.setRequestHeader('Content-length',(params?params.length:0));
  97. xmlHttp.send(params);
Add Comment
Please, Sign In to add comment