繁体   English   中英

AJAX无法获得PHP-echo

[英]AJAX won't get PHP - echo

因此,我正在尝试构建一个非常简单的网站,以学习基本的网页设计。 这只是一个随机的报价生成器-您单击一个按钮,它会打印一些著名的报价。 无论如何,我尝试使用ajax来获取报价,所以我使用了以下功能:

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("quote").innerHTML=xmlhttp.responseText;
            }
    }
    xmlhttp.open("GET","RandomQuote.php",true)
    xmlhttp.send();
}

但是无论我在RandomQuote.php中输入什么内容,即使它类似于:

<?php 
 echo 'Hello, world';
 ?>

引用“ div”中没有任何内容,只是空白。我真的不知道这里出了什么问题。感谢您的帮助!

好吧,我不太确定,但是您在调用函数吗? 您将ajax放在loadXMLDoc()中,因此您可能必须调用它。 另一种方法是将ajax放入addEventListener中,以便当用户单击某项时,它将执行。 如果这不是问题,请尝试确保HTML页面中ID为“ quote”的元素的拼写正确。 有时后一种情况对我而言始终是个问题。

    $.get( "RandomQuote.php", function( data ) {
      $( "#quote" ).html( data );
      console.log( "Load was performed." );
    });

不要忘了包括jQuery。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

这是我的ajax函数,请尝试使用它:

/**
 * @function ajax               Send ajax-request with post
 * @param  {string}   xmlpage   Target url
 * @param  {string}   data      Post parameters (like param1=val1&param2=val2...)
 * @param  {Function} callback
 * @return {null}    
 */
function ajax(xmlpage, data, callback)
{ 
    var xmlh = null;
    if(window.XMLHttpRequest)
        xmlh = new XMLHttpRequest();
    else
        try
        { 
            xmlh = new ActiveXObject("Msxml2.XMLHTTP"); 
        }
        catch(ex) 
        { 
            xmlh = new ActiveXObject("Microsoft.XMLHTTP"); 
        }
    if(xmlh)
    {
        xmlh.open("post", xmlpage, true);
        xmlh.onreadystatechange = function(x) 
                            { 
                                if(xmlh.readyState==4 && typeof callback !== 'undefined') 
                                    callback(xmlh.responseText); 
                            }
        xmlh.setRequestHeader("Accept-Language","ru, en");
        xmlh.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlh.send(data);
    }
}

使用问题中的代码跟踪测试文件。 运行良好。 您是否缺少某些内容或其浏览器问题。 我在Mozilla上进行了测试。 为了确保浏览器无关的代码,请使用jQuery进行Ajax调用

test.html

<html>
<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("quote").innerHTML=xmlhttp.responseText;
            }
    }
    xmlhttp.open("GET","RandomQuote.php",true)
    xmlhttp.send();
}
</script>
<div id="quote"></div>
<input type="button" value="Click Me" onclick="loadXMLDoc()"/>
</html>

RandomQuote.php

<?php

echo 'hi';

更新:jQuery版本

<html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
function loadXMLDoc()
{
    $.get( "RandomQuote.php", function( data ) {
      $( "#quote" ).html( data );
    });

}
</script>
<div id="quote"></div>
<input type="button" value="Click Me" onclick="loadXMLDoc()"/>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM