Ajax的GET传值没有关问题,POST传值有有关问题!帮忙看看!
Ajax的GET传值没问题,POST传值有问题!!!帮忙看看!!!!
如果把这些代码改成GET方式就正常,而用POST方式时,在gethint.php里就获取不到post传来的数据,这是怎么回事?帮忙看看,谢谢!
共三个文件,如下:
1. text.php
2.clienthint.js
3. gethint.php
------解决方案--------------------
....
var url="gethint.php&"+new Date().getTime();
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");//for post
xmlHttp.send(content);
....
function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
if (xmlhttp.status==200||xmlhttp.status==0){
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
}
------解决方案--------------------
把content加进去,别要那个true试试
------解决方案--------------------
就是加个时间戳,防止浏览器缓存
调试:
如果把这些代码改成GET方式就正常,而用POST方式时,在gethint.php里就获取不到post传来的数据,这是怎么回事?帮忙看看,谢谢!
共三个文件,如下:
1. text.php
- PHP code
<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
- PHP code
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.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//for post xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("POST",url,true); 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 code
<?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>"; ?>
------解决方案--------------------
....
var url="gethint.php&"+new Date().getTime();
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");//for post
xmlHttp.send(content);
....
function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
if (xmlhttp.status==200||xmlhttp.status==0){
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
}
------解决方案--------------------
把content加进去,别要那个true试试
------解决方案--------------------
就是加个时间戳,防止浏览器缓存
调试: