ajax經常停在狀態3. 求助阿!
js代碼:
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
var XmlHttpVersions = new Array("MSXML2.XMlHTTP.6.0",
"MSXML2.XMlHTTP.5.0",
"MSXML2.XMlHTTP.4.0",
"MSXML2.XMlHTTP.3.0",
"MSXML2.XMlHTTP",
"Microsoft.XMlHTTP");
for(var i=0;i<XmlHttpVersions.length && !xmlHttp;i++)
{
try
{
xmlHttp = new ActiveXObject(XmlHttpVersions);
}
catch (e) {}
}
}
if(!xmlHttp)
alert("Error crearing XMLHttpRequest");
else
return xmlHttp;
}
function CallServer_sn()
{
var js_ip=Math.random();//document.getElementById("ip").value;
var js_sn=document.getElementById("SN").value;
var js_station=document.getElementById("station").value;
var js_mo=document.getElementById("mo").value;
var js_total=document.getElementById("total_qty").innerHTML;
if(js_total==''){js_total=0}
if (js_sn!="")
{
var url ="do.php?station="+js_station+"&SN="+js_sn+"&mo="+js_mo+"&js_total="+js_total+"&js_ip="+js_ip;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=handleRequestStateChange_name;
xmlHttp.send(null);
document.getElementById("SN").focus()
}else{
nameDiv.innerHTML = "SN 不能為空";
//alert("請輸入SN/SSN﹗");
document.getElementById("SN").focus()
}
}
function handleRequestStateChange_name()
{
nameDiv = document.getElementById("result");
if(xmlHttp.readyState < 4)
{
//nameDiv.innerHTML = "loading..."+xmlHttp.readyState;
}else if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
//nameDiv.innerHTML=xmlHttp.responseTEXT;
var res = xmlHttp.responML.getElementsByTagName('RES');
var mo = xmlHttp.responML.getElementsByTagName('mo');
var station = xmlHttp.responML.getElementsByTagName('station');
var sn = xmlHttp.responML.getElementsByTagName('sn');
var total = xmlHttp.responML.getElementsByTagName('total');
var com = xmlHttp.responML.getElementsByTagName('com');
document.getElementById("SN").value='';
if(res[0].childNodes[0].nodeValue=='OK'){
nameDiv.innerHTML='SN: '+sn[0].childNodes[0].nodeValue+' 結果:'+res[0].childNodes[0].nodeValue//+com[0].childNodes[0].nodeValue
document.getElementById("total_qty").innerHTML=total[0].childNodes[0].nodeValue
document.getElementById("phr").controls.play();
var u="http://localhost:5088/mesracket/S40packinWG/print_barcode.php?command="+com[0].childNodes[0].nodeValue;
xmlHttp.open("GET",u,true);
xmlHttp.onreadystatechange=handleResponse;
xmlHttp.send(null);
}else{
location.replace("error1.php?SN="+sn[0].childNodes[0].nodeValue+"&RES="+res[0].childNodes[0].nodeValue+"&mo="+mo[0].childNodes[0].nodeValue+"&station="+station[0].childNodes[0].nodeValue)
}
}else{
alert("problrm" + e.toString());
}
}
}
處理頁面DO的代碼
header('Content-Type:text/xml;charset=big5');
$SN=strtoupper(trim($_GET['SN']));
$emp='SP3';
$mo=$_GET['mo'];
$station=$_GET['station'];
$js_total=$_GET['js_total'];
$ip=$_GET['js_ip'];
$RES='';$COMM= $SN;
if($SN!='9') $RES='OK';else $RES='error';
if($RES=='OK'){
$js_total=$js_total+1;
$_SESSION['total_right']=$js_total;
$ok=time().rand(100,999);
}
echo '<?xml version="1.0" encoding="BIG5" standalone="yes" ?>';
echo '<serial_number> ';
echo '<sn>'.$SN.'</sn>
<mo>'.$mo.'</mo>
<model>'.$model_name.'</model>
<station>'.$station.'</station>
<total>'.$js_total.'</total>
<com>'.$COMM.'</com>
<ok>'.$ok.'</ok>
<RES>'.$RES.'</RES>';
echo '</serial_number>';
?>