php - 我的 AJAX 文件不起作用

标签 php javascript html ajax

我只是做了一个简单的AJAX应用程序,以便我的html页面可以通过ajax.js文件从php文件中获取一些数据,我已经将它们放在服务器中,所以当我访问localhost/mypage时,应该是已执行,但是似乎存在一些问题,因此事情没有按照我预期的方式发生。

这是我的 html 文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <META http-equiv="Content-Type" Content="text/html; Charset=UTF-8">
    <title>
        My Portal
    </title>
    <link rel="stylesheet" href="style.css" type="text/css"/>
    <script type="text/javascript" src="ajax.js"></script>
</head>
<body onload="load()">
    <div id="header">
        <img src="images/logo.gif" id="logo" alt="Logo"/>
        <a href="http://www.google.com">
            <img src="images/logo2.png" id="logo2" align="right" alt="google Ltd"/>
        </a>
    </div> 
    <div id="container">
        <div id="function"></div>
        <div id="Display"></div>
        <div id="View"></div>
    </div>
    <div id="footer">
    </div>
</body>
</html>

下面是js文件:

function load(){
    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("function").innerHTML=xmlhttp.responseText;         
        }
    }

    xmlhttp.open("GET","test.php", true);
    xmlhttp.send();
}

最后是php文件:

<?php
     print "<h1>hgfkegfalhgj</h1>";
?>

我希望它应该在我的页面中打印出上面的字符串,但是,什么也没有显示,谁能告诉我问题出在哪里?我已经检查过我已经在浏览器中启用了 javascript,所以我很确定我的 html 文件可以调用 js 文件中的函数。

最佳答案

我不懂 PHP,但您必须将 content 类型设置为 text/plain 。喜欢:

header('Content-Type: text/plain');

关于php - 我的 AJAX 文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11465226/

相关文章:

php - TinyMCE 安全问题 : How do you prevent malicious input?

php - 关闭准备好的语句

javascript - DataTables - 如何跨多列进行过滤?

html - 如何让特定的 unicode 字符显示在 Chrome/webkit 浏览器中?

javascript - JQuery - 每 x 次将盒子插入墙壁

php - exec() 通过命令行而不是网络运行

javascript - 我怎样才能从javascript调用flash cs6

javascript - 浏览器中的事件绑定(bind)机制

html - 仅特定 DIV 容器中的垂直滚动条

javascript - 如何从其字形中解码 html 杂项符号?