Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript XAMPP-PHP脚本不会显示在HTML文件中_Javascript_Xmlhttprequest - Fatal编程技术网

Javascript XAMPP-PHP脚本不会显示在HTML文件中

Javascript XAMPP-PHP脚本不会显示在HTML文件中,javascript,xmlhttprequest,Javascript,Xmlhttprequest,我正在运行XAMPP和Apache。我有两个文件: index.php <html> <head> <title>php script</title> </head> <body> <?php header("Access-Control-Allow-Origin: *"); echo "hello world"; ?> </body> </html> php脚本 post.ht

我正在运行XAMPP和Apache。我有两个文件:

index.php

<html>
<head>
<title>php script</title>
</head>
<body>

<?php
header("Access-Control-Allow-Origin: *");
echo "hello world";
?>

</body>
</html>

php脚本
post.html

<html>
<head>
<title>html page</title>
</head>
<body>
<script>

var xhr;
xhr=new XMLHttpRequest();
xhr.open("POST", "http://localhost/demoApp/index.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send();

</script>
</body>
</html>

html页面
var-xhr;
xhr=newXMLHttpRequest();
xhr.open(“POST”http://localhost/demoApp/index.php“,对);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhr.send();
我希望post.html调用index.php,以便post.html在html页面上显示“hello world”。当我打开index.php时,我会看到“hello world”

我还知道post.html正在调用index.php,因为我有其他脚本,如果我打开post.html,当复制到index.php中时,它将成功地向我发送电子邮件


如果post.html确实在调用index.php,为什么我的回音没有显示在post.html上?

post.html正在发出请求,但您实际上没有对该请求做任何事情。使用ajax时,javascript正在进行调用,您需要明确地告诉它您希望对响应执行什么操作

要简单地向远程内容发出警报,请在上收听,并在4(完成)时显示警报:

xhr.onreadystatechange = function() {
    if(xhr.readyState==4) {
        alert(xhr.responseText);
    }
}
如果您希望将数据附加到任何位置(不使用jQuery),我建议您分配一个带有ID的div,并附加或分配数据:

index.php:

<?php
header("Access-Control-Allow-Origin: *");
echo "hello world";
?>

post.html:

<html>
<head>
<title>html page</title>
</head>
<body>
<div id="remote_content"></div>
<script>

var xhr;
xhr=new XMLHttpRequest();
xhr.open("POST", "http://localhost/demoApp/index.php", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
    if(xhr.readyState !== 4)
        return;

    document.getElementById('remote_content').innerHTML = xhr.responseText;
}
xhr.send();

</script>
</body>
</html>

html页面
var-xhr;
xhr=newXMLHttpRequest();
xhr.open(“POST”http://localhost/demoApp/index.php“,对);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhr.onreadystatechange=函数(){
如果(xhr.readyState!==4)
返回;
document.getElementById('remote_content')。innerHTML=xhr.responseText;
}
xhr.send();

谢谢您的帮助。在我所有的研究中,我的理解是,通过发布到包含PHP脚本的服务器,PHP脚本将在我的HTML页面上执行。当直接访问脚本时,这是正确的,而不是在需要告诉脚本如何处理发布到的页面输出的AJAX上下文中。