当前位置: 代码迷 >> Ajax >> 展示不了正确的文本,responseText
  详细解决方案

展示不了正确的文本,responseText

热度:290   发布时间:2012-10-13 11:38:17.0
显示不了正确的文本,responseText
三个文件分别是
l.html
myAJAXlib.js
libtest.php

不能接收html传递的参数param,要怎么写呢?
HTML code
<html>
<head>
<script language="javascript" src="myAJAXlib.js"></script>
<script language="javascript">
function cback(text){
    alert(text);
}
</script>
</head>
<body>
<form name="form1">
<input type="button" value="test" onclick="doAjax('libtest.php','param=hello','cback','get','0')">
</body>
</html>


JScript code
function createREQ(){
    var req=false;
    try{
        req=new XMLHttpRequest();
       } 
    catch(err1)
    {
    try{
        req=new ActiveXObject("Msxml2.XMLHTTP");
       }
    catch(err2)
    {
    try{
        req=new ActiveXObject("Microsoft.XMLHTTP");
       }
    catch(err3)
       {
        req=false;
       }
    }
    }
    return req;
}

function requestGET(url,query,req){
    myRand=parseInt(Math.random()*99999);
    req.open("GET",url+'?'+query+'&rand='+myRand,true);
    req.send(null);
}

function requestPOST(url,query,req){
    req.open("POST",url,true);
    req.setRequestHeader('Content-Type','application.x-www-form-urlencoded');
    req.send(query);
}

function doCallback(callback,item){
    eval(callback+'(item)');
}

function doAjax(url,query,callback,reqtype,getxml){
    var myreq=createREQ();
    myreq.onreadystatechange=function(){
        if(myreq.readyState==4){
        if(myreq.status==200){
            var item=myreq.responseText;
            if(getxml==1){
            item=myreq.responseXML;
        }
    doCallback(callback,item);
}
}
}
if(reqtype=='post'){
requestPOST(url,query,myreq);
}
else{
requestGET(url,query,myreq);
}
}


PHP code
<?php
echo "Parameter value was".$param;
?>


------解决方案--------------------
$_GET是获取GET提交的键值对,$_POST则是POST提交的

ajax提交的和普通form提交的或者直接在url后加参数【get】获取参数的方法一样