0

I have an error with AJAX xml response. When I call function get_res_table() I'm getting error as in the header.

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Actions</title>
  </head>
 <body>  
  <fieldset>

   <legend>Actions</legend>

   <label>Pick Search:</label>

   <form name="form1" method="get">

   <select id="Show" name="Show">
    <option selected value="0">Nurse wards</option>
    <option value="1">Wards by department</option>
    <option value="2">Wards by shift</option>
    </select>

    <input type="button" value="Select search type" onClick="javascript: gets2();"/>

     <div id="Search_type" style="display: none;">
     <hr/>

     <label>Pick object:</label>
     <select id="Search_val" name="Search_val">
     </select>

     <input type="button" value="Find" onClick="javascript: gets_res_table();"/>
     <hr />
     </div>

     <div id="date"></div>
     <div id="department"></div>
     <div id="shift"></div>
     </form>

  </fieldset>
 </body>
</html>

<script type="text/javascript">
 function gets2()
 {
  if (!ajax)
   { 
     alert("Ajax not initialized");
     return; 
   }

  var s1val = document.getElementById("Show").value;

  ajax.onreadystatechange = UpdateSelect2;
  ajax.open("GET", "select.php?select1="+s1val, true);
  ajax.send('localhost'); 
 }
</script>

<script type="text/javascript">
 var ajax;

 InitAjax();

 function InitAjax()
 {
   try
   {
     ajax = new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (e)
   {
     try
     {
       ajax = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
       try
       {
         ajax = new XMLHttpRequest();
       }
       catch (e)
       {
         ajax = 0;
       }
     }
   }
 }
</script>

<script type="text/javascript">
 function UpdateSelect2()
 {
   if (ajax.readyState == 4) 
   {
     if (ajax.status == 200) 
     {
       var divBody = document.getElementById('Search_val');
       divBody.innerHTML = ajax.responseText;
       document.getElementById('Search_type').style.display = "block";
     }
     else alert(ajax.status + " - " + ajax.statusText);
     ajax.abort();
   }
 }
</script>

<script type="text/javascript">
 function gets_res_table()
 {
   if (!ajax)
   { 
     alert("Ajax not initialized");
     return;
   }

   var s1val = document.getElementById("Show").value;
   var obj = document.getElementById("Search_val").value;

   ajax.onreadystatechange = UpdatePage;
   ajax.open("GET", "result.php?select1="+s1val+"&obj="+obj, true);
   ajax.send('localhost'); 
  }
</script>

<script type="text/javascript">
 function UpdatePage()
 {
   if(ajax.readyState == 4) 
   {
     if(ajax.status == 200) 
     {
       xmlDoc=ajax.responseXML;
       document.getElementById("date").innerHTML =
       xmlDoc.getElementsByTagName("date")[0].childNodes[0].nodeValue;
       document.getElementById("department").innerHTML =
       xmlDoc.getElementsByTagName("department")[0].childNodes[0].nodeValue;
       document.getElementById("shift").innerHTML =
       xmlDoc.getElementsByTagName("shift")[0].childNodes[0].nodeValue;
     }
   }
   else
   {
     alert(ajax.status + " - " + ajax.statusText);
     ajax.abort();
   }
 }
</script>

It worked some time when I was executing it in another(New) project or something like that. But now it don't works at all and always throws me such error.

KofHein
  • 59
  • 10
  • Why are you calling abort? – epascarello Nov 13 '15 at 19:08
  • erm it is there...i don't have any other..only .php for result. That is all my code and javascript is here – KofHein Nov 13 '15 at 19:09
  • abort because it failed...for example responseText works fine and such code works really well with responseText, but I need to do it with xml... – KofHein Nov 13 '15 at 19:10
  • You should do open before handling onreadystatechange... – rby Nov 13 '15 at 19:14
  • If the request failed, it will not be open, so calling abort is not needed. You would call it if the request is open and you need to cancel. – epascarello Nov 13 '15 at 19:16
  • Please have a look at how to create a [mcve]. Also [indent](https://en.wikipedia.org/wiki/Indentation_(typesetting)) your code for readability. – Felix Kling Nov 13 '15 at 19:25
  • @rby your advice helped, but now it shows dialog box with code 200-OK and don't get in block with tag processing.. – KofHein Nov 13 '15 at 19:26
  • @FelixKling yeah i know that code formatting is terrible.. – KofHein Nov 13 '15 at 19:27
  • If you expect people to read your question, understand your code and help, then the least you can do is make it easier for them. If you put more effort into your question then more people are willing to put effort into helping you. – Felix Kling Nov 13 '15 at 19:28
  • Check your value for readyState, in addition to status. Also, since you are using GET, it should really be send() – rby Nov 13 '15 at 19:48
  • Does this error happen at the first request already, or only on the second one? If you are trying to improve performance by “re-using” one single `XMLHttpRequest` object, there’s suggestions to rather not do that: http://stackoverflow.com/a/11079569/1427878, http://stackoverflow.com/a/11502446/1427878 – CBroe Nov 13 '15 at 19:52
  • yeah I've already done it) if i do it your way - open before event handler, then readyState 3 is never shown.. – KofHein Nov 13 '15 at 19:53
  • @CBroe no, it don't works at all...if i use open before event handler, then readyState 3 is never shown, and if I'm calling this function again I'm getting error as in the header. For my code example this error is thrown at execution of function – KofHein Nov 13 '15 at 19:56
  • okay I've rewrote it and now it works, code is the same, I don't see any differences. – KofHein Nov 14 '15 at 07:45

0 Answers0