AJAX的post有问题,get没问题!!!
我测试了status和readyState的值都正常,纳闷的是get方式传值却很正常,帮忙看看怎么回事?
有三个文件,如下:
1.text.php
- <html>
- <head>
- <script src="clienthint.js"></script>
- </head>
- <body>
- <form>
- First Name:
- <input type="text" id="txt1">
- <input type="button" value="Submit" onClick="showHint()">
- </form>
- <p>Suggestions: <span id="txtHint"></span></p>
- </body>
- </html>
复制代码2.clienthint.js
- var xmlHttp
- function showHint()
- {
- var str = document.getElementById("txt1").value;
- if (str.length==0)
- {
- document.getElementById("txtHint").innerHTML="";
- return;
- }
- xmlHttp=GetXmlHttpObject();
- if (xmlHttp==null)
- {
- alert ("Browser does not support HTTP Request");
- return;
- }
- var url="gethint.php";
- var content="qt="+str;
- content=content+"&sid="+Math.random();
- xmlHttp.open("POST",url,true);
- xmlHttp.onreadystatechange=stateChanged;
- xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
- xmlHttp.send(content);
- }
- function stateChanged()
- {
- if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
- {
- document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
- }
- }
- function GetXmlHttpObject()
- {
- var xmlHttp=null;
- try
- {
- // Firefox, Opera 8.0+, Safari
- xmlHttp=new XMLHttpRequest();
- }
- catch (e)
- {
- // Internet Explorer
- try
- {
- xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
- }
- catch (e)
- {
- xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- }
- return xmlHttp;
- }
复制代码3.gethint.php
- <?php
- // Fill up array with names
- $a[]="Anna";
- $a[]="Brittany";
- $q = $_POST["qt"];
- if (strlen($q) > 0)
- {
- $hint="";
- for($i=0; $i<count($a); $i++)
- {
- if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
- {
- if ($hint=="")
- {
- $hint=$a[$i];
- }
- else
- {
- $hint=$hint." , ".$a[$i];
- }
- }
- }
- }
- else echo "q is empty!<br>";
- //Set output to "no suggestion" if no hint were found
- //or to the correct values
- if ($hint == "")
- {
- $response="no suggestion";
- }
- else
- {
- $response=$hint;
- }
- //output the response
- echo $response;
- echo " to ";
- echo $q;
- echo "<br>";
- ?>
复制代码
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没人顶!!!!!