我正在构建一个PHP脚本,可以在生日那天向人们发送电子邮件。我还需要一个小型的网络界面,我可以打开或关闭整个程序,观看谁今天庆祝他的生日,看看是否有任何邮件无法发送。服务器上的PHP正在运行,但我正在努力使用html和javascript界面。特别是从服务器获取信息的Ajax请求。它在firefox,chrome和opera中运行良好,但在Internet Explorer 8中我没有得到服务器的响应。 readyState切换为4,但状态保持为0且responseText为空。
谷歌搜索后我发现有很多人建议使用JQuery,但我无法在任何浏览器中使用它。我想了解更多关于它的信息,因为它看起来很简单,但是现在我想知道如何在没有JQuery的情况下做到这一点。
修改
在回答评论中的问题时,切换if和else语句会产生相同的结果。正如改变从“打开”到“关闭”的连接一样,顺便说一句,它应该是接近的,我不记得为什么我改变它,可能只是尝试一些挫折。最后我添加了服务器端的PHP代码。如果'action'是switchonoff,服务器发回字符串数据,或者如果action是clearlog则只返回没有文本的头,如果action是初始化则返回jsonarray。无论请求是什么,服务器的HTTP状态始终为0。
<script type="text/javascript">
function loadXMLDoc(action) {
var parameters = "action="+encodeURI(action);
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.open("POST",'http://mailer.test/App/postscript.php',true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", parameters.length);
xmlhttp.setRequestHeader("Connection", "open");
xmlhttp.send(parameters);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status==200) {
if(action == 'switchonoff'){
document.getElementById("onoffbutton").innerHTML=xmlhttp.responseText;
} else if(action == 'initialize'){
var response = JSON.parse(xmlhttp.responseText);
document.getElementById("onoffbutton").innerHTML=response['onoff'];
document.getElementById("birthdays").innerHTML=response['birthdays'];
document.getElementById("errors").innerHTML=response['errors'];
} else if(action == 'clearlog'){
document.getElementById("errors").innerHTML="";
}
}
}
}
window.onload = function() {
loadXMLDoc('initialize');
}
</script>
编辑,这是php脚本
<?php
include "settingschanger.php";
include "customercsv.php";
if (!isset($_POST['action'])) {
$_POST['action'] = 'dummy';
}
$post = $_POST['action'];
if(strcmp($post, "initialize") == 0) {
$settings = new SettingsChanger();
$onoff = $settings->getOnOff();
$csv = new CustomerCSV();
$csv->openFile((__DIR__).CustomerCSV::FILENAME);
$todaysbirthdays = $csv->getTodaysBirthdays();
$birthdays = "";
foreach ($todaysbirthdays as $row) {
$birthday = "";
foreach ($row as $data) {
$birthday .= $data . " ";
}
$birthdays .= $birthday . "<br />";
}
$errorLogArray = $settings->getErrorLog();
$errors = "";
foreach($errorLogArray as $line) {
$errors .= $line . "<br />";
}
$result = json_encode(array('onoff'=>$onoff, 'birthdays'=>$birthdays, 'errors'=>$errors));
header("HTTP/1.1 200 OK");
print $result;
}
if(strcmp($post, "switchonoff") == 0) {
$settings = new SettingsChanger();
$result = $settings->changeOnOff();
header("HTTP/1.1 200 OK");
print $result;
}
if(strcmp($post, "clearlog") == 0) {
$settings = new SettingsChanger();
$settings->clearLog();
header("HTTP/1.1 200 OK");
}
?>
答案 0 :(得分:0)
在xmlhttp.send(parameters);
声明之后尝试xmlhttp.onreadystatechange
。
答案 1 :(得分:0)
我在旧版IE(低于v9)中返回的内容为空时遇到了一些问题,但触发了成功功能。
然后我发现我正在使用HTML5标签,而在IE中,由于某些奇怪的原因,通过AJAX请求输出的HTML5不起作用并返回空的或没有DOM。
查看“jquery .load() not inserting data in ie8 and below”中的最后一次修改。
答案 2 :(得分:0)
好吧,我让网站上班了。我现在使用get请求而不是发布请求,这似乎有效。它并不完美,我会继续寻找一种方法来处理邮件请求,但现在它可以工作。