AJAX:Uncaught InvalidStateError:无法在'XMLHttpRequest'上执行'send':对象的状态必须是OPENED

时间:2015-11-13 19:04:46

标签: javascript ajax

我在AJAX xml响应时出错。当我调用函数get_res_table()时,我会收到标题中的错误。

<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>

当我在另一个(新)项目或类似项目中执行它时,它工作了一段时间。但现在它根本不起作用,总是把这种错误抛给我。

0 个答案:

没有答案