Ajax POST 请求在 Internet Explorer 中不起作用

Ajax POST request not working in Internet Explorer

本文关键字:Explorer 不起作用 Internet POST 请求 Ajax      更新时间:2023-09-26

我正在构建一个PHP脚本,可以在人们生日时向他们发送电子邮件。我还需要一个小型网络界面,我可以在其中打开或关闭整个程序,观察今天谁在庆祝他的生日,看看是否有任何邮件无法发送。服务器上的 php 正在工作,但我正在努力解决 html 和 javascript 界面。特别是从服务器获取信息的 Ajax 请求。它在Firefox,chrome和Opera中工作正常,但在Internet Explorer 8中,我没有得到服务器的响应。readyState 切换到 4,但状态仍为 0,响应文本为空。

在谷歌搜索之后,我发现很多人建议JQuery,但我无法在任何浏览器中使用它。我想了解更多有关它的信息,因为它看起来很容易,但现在我想知道如何在没有 JQuery 的情况下做到这一点。

编辑

在回答评论中的问题时,切换 if 和 else 语句会产生相同的结果。就像将连接从"打开"更改为"关闭"一样,顺便说一句,它应该是接近的,我不记得我为什么改变它,可能只是出于沮丧而尝试一些事情。最后,我添加了服务器端的php代码。服务器发回字符串数据,以防"action"关闭,或者,如果操作是clearlog,则只有一个没有文本的标头,如果操作初始化,则发送一个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");
}
?>

在语句xmlhttp.onreadystatechange后面尝试使用xmlhttp.send(parameters);

我在旧版本的 IE(低于 v9)中返回的内容为空时遇到了一些问题,但触发了成功函数。

然后我发现我使用的是HTML5标签,而在IE中,由于某种奇怪的原因,通过AJAX请求输出的HTML5不起作用,并返回一个空的或没有DOM。

查看"jquery .load() 未在 ie8 及更低版本中插入数据"中的最后一次编辑。

好吧,我让网站工作了。我现在使用获取请求而不是 post 请求,这似乎有效。它并不完美,我会继续寻找一种通过发布请求来做到这一点的方法,但现在它会起作用。