1

我不确定这是否与 ajax 调用有关。我对 Ajax 很陌生,所以我怀疑这是原因。

我运行以下 javascript:

function GetXmlHttpObject() {
"use strict";
var objXMLHttp = null;

if (window.XMLHttpRequest) {
    objXMLHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    objXMLHttp = new window.ActiveXObject("Microsoft.XMLHTTP");
}

return objXMLHttp;
}

function delete_director(i) {
"use strict";
var r, url;

r = window.confirm("Are you sure you want to disable this director");
url = "ajax.php?task=director&event=delete&UserId=" + i;

if (r === true) {
    mdata = new GetXmlHttpObject();

    if (mdata === null) {
        alert("Browser does not support HTTP Request");
        return;
    }

    mdata.open("GET", url, true);
    mdata.send(null);
}
}

这会调用以下 php 函数:

function deletedirector()
{
    $UserId=mysql_real_escape_string($_GET['UserId']);
    $query = "update tbl_users set IsDisabled='1' where UserId=".$UserId;
    $result = mysql_query($query) OR die('Cannot perform query!');
    if ($result) {
        error_log("a");
        ?><script type="text/javascript">window.location='index.php?task=director&success=Director Successfully Deleted.'</script><?
    } else {
        error_log("b");
        ?><script type="text/javascript">window.location='index.php?task=director&error=Director Deletion Failed.'</script><?
    }
}

db 显示director 已被删除,错误日志中显示“a”,但window.location 永远不会触发。

用户体验是浏览器提示确认,然后 - 什么都没有。JavaScript 控制台现在显示错误。

有任何想法吗?

4

1 回答 1

1

您已经返回带有函数的新对象(XMLHttpRequest API),所以这里不需要new

...
if (r === true) {
    mdata = GetXmlHttpObject();
...

并尝试像这样使用onreadystatechange

mdata.onreadystatechange = function(){
    if (mdata.readyState === 4) {
        alert("some text");
    } else {
        alert(mdata.status);
    }
};
于 2013-04-03T22:38:12.497 回答