AJAX的post有问题,get没问题!!!

我测试了status和readyState的值都正常,纳闷的是get方式传值却很正常,帮忙看看怎么回事?

有三个文件,如下:

1.text.php
  1. <html>
  2. <head>
  3. <script src="clienthint.js"></script>
  4. </head>
  5. <body>
  6. <form>
  7. First Name:
  8. <input type="text" id="txt1">
  9. <input type="button" value="Submit" onClick="showHint()">
  10. </form>
  11. <p>Suggestions: <span id="txtHint"></span></p>
  12. </body>
  13. </html>
复制代码
2.clienthint.js
  1. var xmlHttp
  2. function showHint()
  3. {
  4. var str = document.getElementById("txt1").value;
  5. if (str.length==0)
  6.   {
  7.   document.getElementById("txtHint").innerHTML="";
  8.   return;
  9.   }
  10. xmlHttp=GetXmlHttpObject();
  11. if (xmlHttp==null)
  12.   {
  13.   alert ("Browser does not support HTTP Request");
  14.   return;
  15.   }
  16. var url="gethint.php";
  17. var content="qt="+str;
  18. content=content+"&sid="+Math.random();
  19. xmlHttp.open("POST",url,true);
  20. xmlHttp.onreadystatechange=stateChanged;
  21. xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  22. xmlHttp.send(content);
  23. }

  24. function stateChanged()
  25. {
  26. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  27. {
  28. document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
  29. }
  30. }

  31. function GetXmlHttpObject()
  32. {
  33. var xmlHttp=null;
  34. try
  35. {
  36. // Firefox, Opera 8.0+, Safari
  37. xmlHttp=new XMLHttpRequest();
  38. }
  39. catch (e)
  40. {
  41. // Internet Explorer
  42. try
  43.   {
  44.   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  45.   }
  46. catch (e)
  47.   {
  48.   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  49.   }
  50. }
  51. return xmlHttp;
  52. }
复制代码
3.gethint.php
  1. <?php
  2. // Fill up array with names
  3. $a[]="Anna";
  4. $a[]="Brittany";

  5. $q = $_POST["qt"];
  6. if (strlen($q) > 0)
  7. {
  8. $hint="";
  9. for($i=0; $i<count($a); $i++)
  10.   {
  11.   if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
  12.     {
  13.     if ($hint=="")
  14.       {
  15.       $hint=$a[$i];
  16.       }
  17.     else
  18.       {
  19.       $hint=$hint." , ".$a[$i];
  20.       }
  21.     }
  22.   }
  23. }
  24. else echo "q is empty!<br>";
  25. //Set output to "no suggestion" if no hint were found
  26. //or to the correct values
  27. if ($hint == "")
  28. {
  29. $response="no suggestion";
  30. }
  31. else
  32. {
  33. $response=$hint;
  34. }

  35. //output the response
  36. echo $response;
  37. echo " to ";
  38. echo $q;
  39. echo "<br>";
  40. ?>
复制代码

作者: hua9537   发布时间: 2011-06-06


没人顶!!!!!

作者: hua9537   发布时间: 2011-06-07