AJAX:responseText的地方返回我的整个PHP code [英] ajax: responsetext returns my entire php code locally
问题描述
我找到了一个很好的教程:教程
,但它不本地工作。 存在的问题是,这个responseText返回我的整个PHP code。 我双击我的ajaxclock.html和使用Firefox。 出人意料的是,它工作在服务器上。
下面的code: ajaxclock.html
<!DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< HTML的xmlns =http://www.w3.org/1999/xhtmlXML:LANG =ENLANG =EN>
< HEAD>
<冠军> AJAX教程< /标题>
< /头>
<身体GT;
< DIV ID =时间>< / DIV>
<按钮的onclick =的getTime();> Aktualisieren< /按钮>
<脚本类型=文/ JavaScript的SRC =的script.js>< / SCRIPT>
< /身体GT;
< / HTML>
的script.js
VAR REQ = getXmlHtt prequestObject();
在window.onload =的getTime();
功能getXmlHtt prequestObject()
{
如果(window.XMLHtt prequest)
{
返回新XMLHtt prequest();
}
否则,如果(window.ActiveXObject)
{
返回新的ActiveXObject(Microsoft.XMLHTTP);
}
其他
{
警报('阿贾克斯funktioniert贝Ihnen nicht!');
}
}
函数的getTime()
{
如果(req.readyState == 4 || req.readyState == 0)
{
req.open(GET,ajaxclock.php,真正的);
req.setRequestHeader(内容类型,text / plain的);
req.onreadystatechange = setMessage;
req.send(空);
}
}
函数setMessage()
{
如果(req.readyState == 4)
{
变种响应=的eval('('+ req.responseText +')');
的document.getElementById(时间)的innerHTML = response.time。
}
}
ajaxclock.php
< PHP的回声{时间:'.date(H:我:秒)。'}'?; ?>
令人惊讶的,它工作在服务器上。
PHP是一种服务器端技术。这将只工作一台服务器(具体配置为运行PHP程序的服务器)上。
如果你使用它没有这样的服务器,则什么都不会执行PHP,它会被传递到其原始状态的浏览器。
I found a good Tutorial: tutorial
but it doesn't work locally. The Problem is, that the responsetext returns my entire php code. I double-click at my ajaxclock.html and use Firefox. Surprisingly, it works on the server.
Here the code: ajaxclock.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>AJAX Tutorial</title>
</head>
<body>
<div id="time"></div>
<button onclick="getTime();">Aktualisieren</button>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
script.js
var req = getXmlHttpRequestObject();
window.onload = getTime();
function getXmlHttpRequestObject()
{
if(window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert('Ajax funktioniert bei Ihnen nicht!');
}
}
function getTime()
{
if(req.readyState == 4 || req.readyState == 0)
{
req.open('GET', 'ajaxclock.php', true);
req.setRequestHeader("Content-Type","text/plain");
req.onreadystatechange = setMessage;
req.send(null);
}
}
function setMessage()
{
if(req.readyState == 4)
{
var response = eval('(' + req.responseText+ ')');
document.getElementById('time').innerHTML = response.time;
}
}
ajaxclock.php
<?php echo '{"time": "'.date("H:i:s").'"}'; ?>
Surprisingly, it works on the server.
PHP is a server side technology. It will only ever work on a server (specifically a server configured to run PHP programs).
If you use it without such a server then nothing will execute the PHP and it will be delivered to the browser in its raw state.
这篇关于AJAX:responseText的地方返回我的整个PHP code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!