Javascript ajax php调用不返回任何内容

Javascript ajax php调用不返回任何内容,javascript,php,ajax,Javascript,Php,Ajax,我希望php向ajax返回一个值。我以W3学校为例,但没有乐趣。 以下是javascript/ajax代码: function createReport() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for I

我希望php向ajax返回一个值。我以W3学校为例,但没有乐趣。 以下是javascript/ajax代码:

function createReport() {
    var xmlhttp;    
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("report").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","test.php",true);
    xmlhttp.send();
}
在我知道正在触发的事件处理程序中有以下调用(它所做的其他工作正常)

在html的正文部分,我有:

<div id="report">Report will be placed here...</div>
报告将放置在此处。。。
如果我自己运行test.php,它会正确地显示“returnthistoajax”,所以我知道这是可行的。以下是php代码:

<?php
echo 'return this to ajax';
?>

我的理解是,“报告将放在这里…”将替换为“将其返回到ajax”。但什么也没发生。我也没有看到firefox或IE控制台中列出任何错误。有什么想法吗

try{
    request = new XMLHttpRequest();
}catch(trymicrosoft){
     try{
        request = new ActiveXObject("Msxml2.XMLHTTP");
     }catch(othermicrosoft){
        try{
            request = new ActiveXObject("Microsoft.XMLHTTP");
       }catch(failed){
            request = false;
       }  
    }
}
if(!request)
{
    alert("Error initializing XMLHttpRequest!");
}

function Create_Ajax_Query(LinkToFile,Parametrs)
{
    window.document.body.style.cursor='progress';
    request = new XMLHttpRequest();
    var url = LinkToFile;
    var params = Parametrs;

    request.open("POST", url, true);    
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  
    request.setRequestHeader("Content-length", params.length);
    request.setRequestHeader("Connection", "close");
    request.onreadystatechange = newinfo;
    request.send(params); 
 }

function newinfo() 
{
    if(request.readyState==4 && request.status == 200){
        window.document.body.style.cursor='default';
        alert(request.responseText);
    } 
}
希望它有用

希望它有用

希望它有用


希望它有用

乍一看,我没有发现您的js代码有任何错误,所以我打赌在您的文件夹结构中查找test.php可能会有问题


使用firebug检查调用javascript AJAX的情况,并检查文件test.php是否正确评估。

乍一看,我没有发现js代码有任何错误,因此我打赌在文件夹结构中查找test.php可能会有问题


使用firebug检查调用javascript AJAX的情况,并检查文件test.php是否正确评估。

乍一看,我没有发现js代码有任何错误,因此我打赌在文件夹结构中查找test.php可能会有问题


使用firebug检查调用javascript AJAX的情况,并检查文件test.php是否正确评估。

乍一看,我没有发现js代码有任何错误,因此我打赌在文件夹结构中查找test.php可能会有问题


使用firebug检查javascript AJAX的调用情况,并检查test.php文件是否正确评估。

我个人认为W3CSchool不是学习的好地方(请参阅)

问题可能是由于您设置ajax的顺序造成的,您做到了:

XMLHttpRequest/onreadystatechange/open/send

更可取(如果不遵循此顺序,旧浏览器中可能会出现一些缺陷):

XMLHttpRequest/open/onreadystatechange/send

注意:不要担心,“打开(…)”不会启动侦听器。 侦听器仅在“发送(…)”之后工作

另一个原因可能是您没有创建
XMLHttpRequest.status
的“错误处理”,它用于验证服务器响应中的错误

试试这个:

<script>
function XHR(){
    if(window.XMLHttpRequest){//outers browsers
        return new XMLHttpRequest();
    } else if(window.ActiveXObject){//IE
        try{
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(ee) {//IE
            try{
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch(ee) {}
        }
    }
    return false;
}

function createReport(){
    var xhr = XHR();// call ajax object
    if(xhr){
        xhr.open("GET", "test.php", true);//setting request (should always come first)
        xhr.onreadystatechange = function(){//setting callback
            if(xhr.readyState==4){
                if(xhr.status==200){
                    document.getElementById("report").innerHTML=xhr.responseText;
                } else {//if the state is different from 200, is why there was a server error (eg. 404)
                    alert("Server return this error: " + String(xhr.status));
                }
            }
        };
        xhr.send(null);//send request, should be the last to be executed.
    } else {
        alert("Your browser no has support to Ajax");
    }
}
</script>

<div id="report">Report will be placed here...</div>

<script>
createReport();//prefer to call the function after its div#report
</script>

函数XHR(){
if(window.XMLHttpRequest){//outers浏览器
返回新的XMLHttpRequest();
}如果(window.ActiveXObject){//IE
试一试{
返回新的ActiveXObject(“Msxml2.XMLHTTP”);
}抓住(ee){//IE
试一试{
返回新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(ee){}
}
}
返回false;
}
函数createReport(){
var xhr=xhr();//调用ajax对象
if(xhr){
open(“GET”,“test.php”,true);//设置请求(应始终放在第一位)
xhr.onreadystatechange=函数(){//设置回调
if(xhr.readyState==4){
如果(xhr.status==200){
document.getElementById(“报告”).innerHTML=xhr.responseText;
}否则{//如果状态不同于200,则是出现服务器错误的原因(例如404)
警报(“服务器返回此错误:+字符串(xhr.status));
}
}
};
send(null);//send请求应该是最后执行的。
}否则{
警报(“您的浏览器不支持Ajax”);
}
}
报告将放在这里。。。
createReport()//更喜欢在函数的div#报告之后调用该函数
要防止缓存,请替换:

xhr.open(“GET”,“test.php”,true)


xhr.open(“GET”,“test.php?nocache=“+(new Date().getTime()),true)

我个人认为W3C学校不是一个学习的好地方(请参阅)

问题可能是由于您设置ajax的顺序造成的,您做到了:

XMLHttpRequest/onreadystatechange/open/send

更可取(如果不遵循此顺序,旧浏览器中可能会出现一些缺陷):

XMLHttpRequest/open/onreadystatechange/send

注意:不要担心,“打开(…)”不会启动侦听器。 侦听器仅在“发送(…)”之后工作

另一个原因可能是您没有创建
XMLHttpRequest.status
的“错误处理”,它用于验证服务器响应中的错误

试试这个:

<script>
function XHR(){
    if(window.XMLHttpRequest){//outers browsers
        return new XMLHttpRequest();
    } else if(window.ActiveXObject){//IE
        try{
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(ee) {//IE
            try{
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch(ee) {}
        }
    }
    return false;
}

function createReport(){
    var xhr = XHR();// call ajax object
    if(xhr){
        xhr.open("GET", "test.php", true);//setting request (should always come first)
        xhr.onreadystatechange = function(){//setting callback
            if(xhr.readyState==4){
                if(xhr.status==200){
                    document.getElementById("report").innerHTML=xhr.responseText;
                } else {//if the state is different from 200, is why there was a server error (eg. 404)
                    alert("Server return this error: " + String(xhr.status));
                }
            }
        };
        xhr.send(null);//send request, should be the last to be executed.
    } else {
        alert("Your browser no has support to Ajax");
    }
}
</script>

<div id="report">Report will be placed here...</div>

<script>
createReport();//prefer to call the function after its div#report
</script>

函数XHR(){
if(window.XMLHttpRequest){//outers浏览器
返回新的XMLHttpRequest();
}如果(window.ActiveXObject){//IE
试一试{
返回新的ActiveXObject(“Msxml2.XMLHTTP”);
}抓住(ee){//IE
试一试{
返回新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(ee){}
}
}
返回false;
}
函数createReport(){
var xhr=xhr();//调用ajax对象
if(xhr){
open(“GET”,“test.php”,true);//设置请求(应始终放在第一位)
xhr.onreadystatechange=函数(){//设置回调
if(xhr.readyState==4){
如果(xhr.status==200){
document.getElementById(“报告”).innerHTML=xhr.responseText;
}否则{//如果状态不同于200,则是出现服务器错误的原因(例如404)
警报(“服务器返回此错误:+字符串(xhr.status));
}
}
};
send(null);//send请求应该是最后执行的。
}否则{
警报(“您的浏览器不支持Ajax”);
}
}
报告将放在这里。。。
createReport()//更喜欢在函数的div#报告之后调用该函数
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("report").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","test.php",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="report"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>
<?php
echo 'return this to ajax';
?>
function createReport() {
    var xmlhttp;    
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        console.log(xmlhttp); //to show the entire object after statechage.
        console.log(xmlhttp.readyState); //to show specific parameter.        

        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("report").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","test.php",true);
    xmlhttp.send();
}