使用HTMLrequest从Javascript调用PHP文件在xampp localserver上不起作用
我正在尝试从使用HTMLrequest从Javascript调用PHP文件在xampp localserver上不起作用,javascript,php,html,Javascript,Php,Html,我正在尝试从Javascript运行一个php文件。我正在使用XAMPP服务器,并将所有文件保存在htdocs文件夹中。PHP文件也保存在htdocs文件夹中,并且可以使用以下地址正常工作http://localhost/php_test.php在chrome浏览器中 正在使用的HTML代码写在下面 <html> <body></body> <script> getOutput(); function getOutput() { getReques
Javascript
运行一个php文件。我正在使用XAMPP服务器,并将所有文件保存在htdocs
文件夹中。PHP文件也保存在htdocs文件夹中,并且可以使用以下地址正常工作http://localhost/php_test.php
在chrome浏览器中
正在使用的HTML代码写在下面
<html>
<body></body>
<script>
getOutput();
function getOutput() {
getRequest(
"php_test.php", // URL for the PHP file
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
// handles drawing an error message
function drawError() {
}
// handles the response, adds the html
function drawOutput(responseText) {
}
// helper function for cross-browser request object
function getRequest(url, success, error) {
var req = false;
try{
req = new XMLHttpRequest();
} catch (e){
// IE
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
// try an older version
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
return false;
}
}
}
if (!req) return false;
if (typeof success != 'function') success = function () {};
if (typeof error!= 'function') error = function () {};
req.onreadystatechange = function(){
if(req.readyState == 4) {
return req.status === 200 ?
success(req.responseText) : error(req.status);
}
}
req.open("GET", url, true);
req.send(null);
return req;
}
</script>
</html>
getOutput();
函数getOutput(){
获取请求(
“php_test.php”//php文件的URL
drawOutput,//处理成功的请求
抽屉错误//句柄错误
);
返回false;
}
//处理绘制错误消息
函数drawError(){
}
//处理响应,添加html
函数drawOutput(responseText){
}
//跨浏览器请求对象的帮助器函数
函数getRequest(url、成功、错误){
var-req=false;
试一试{
req=新的XMLHttpRequest();
}捕获(e){
//即
试一试{
req=新的ActiveXObject(“Msxml2.XMLHTTP”);
}捕获(e){
//试试旧版本
试一试{
req=新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(e){
返回false;
}
}
}
如果(!req)返回false;
if(typeof success!=“function”)success=function(){};
if(typeof error!=“function”)error=function(){};
req.onreadystatechange=函数(){
如果(req.readyState==4){
返回请求状态===200?
成功(请求响应文本):错误(请求状态);
}
}
请求打开(“获取”,url,true);
请求发送(空);
返回请求;
}
PHP文件是
<?php
echo 'hello world!';
?>
但是,运行html文件不会在浏览器中显示任何输出。我已尝试调试代码并在inspect元素中检查它,但看不到任何问题或错误。您的函数
drawError
和drawOutput
为空,您认为它们如何打印任何内容
function drawError() {
}
function drawOutput(responseText) {
}
试着这样做:
function drawError(error) {
document.write(error);
}
function drawOutput(responseText) {
document.write(responseText);
}
您的函数
drawerrror
和drawOutput
为空,您认为它们如何打印任何内容
function drawError() {
}
function drawOutput(responseText) {
}
试着这样做:
function drawError(error) {
document.write(error);
}
function drawOutput(responseText) {
document.write(responseText);
}
尝试将
php\u test.php
更改为http://localhost/php_test.php
函数drawOutput(responseText){}
此函数在将php\u test.php
更改为http://localhost/php_test.php
函数drawOutput(responseText){}
此功能在内部不起任何作用