Php AJAX没有响应
所以我试图从我的php文件中提醒我的“你好”,这很简单,但由于某些原因它不起作用 JSPhp AJAX没有响应,php,javascript,html,ajax,Php,Javascript,Html,Ajax,所以我试图从我的php文件中提醒我的“你好”,这很简单,但由于某些原因它不起作用 JS function dynamic() { if(window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onready
function dynamic()
{
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
window.alert(xmlhttp.responseText);
}
}
xmlhttp.open("POST", "hello.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
dynamic();
PHP
<?php
echo "Hello";
?>
HTML
<html>
<head>
<title>Hello</title>
<script src="dynamic.js"></script>
</head>
<body>
test
</body>
</html>
你好
测试
我无法让提醒框提醒我的“你好”。它应该直接来自responseText。您没有调用
send
方法。它发送请求
若要将任何数据放入POST,请将其作为urlencoded格式放入send methods参数中
xmlhttp.send("foo=bar&hello=world");
试试这个:
function dynamic()
{
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
window.alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "hello.php", true);
xmlhttp.send(null);
}
dynamic();
我想你忘了发送请求:
xmlhttp.send();
试试这个
xmlhttp.setRequestHeader("Content-type", "text/plain");
或
而不是
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
你必须把
xmlhttp.send(null);
此外,在类似的情况下,登录php文件是一种有用的行为。所以我总是要发送一些东西,即使是空的?@Howdy_McGee是的,因为它发送requests@david没错。我总是用
null
表示GET,所以我写了这个。实际上它并不需要它。只是出于好奇:如果你不发送任何数据,为什么要使用POST请求?GET更简单、更直接。
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(null);