XMLHttpRequest有时不发送字符串


XMLHttpRequest sometimes doesn't send string

我不确定为什么会发生这种情况,但我会尽可能多地解释我已经知道的。我有一个网站,允许您登录与存储在数据库中的帐户。每个用户都可以更新自己的数据集,这些数据集也在同一个数据库中。当用户更改数据时,Javascript将向服务器上的php文件发送XMLHttpRequest。数据是JSON编码的,在php文件中解码,然后将数据存储在数据库中。

问题是每当我登录到一个特定的帐户没有数据发送。发送请求后,字符串为空。postworks和php文件运行,但没有数据存在。以下是我在JS中发送请求的代码:

function sendXMLHttpRequest(data, phpfile){
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }
    xhr.open('POST', phpfile, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                if (this.responseText !== null)
                    document.getElementById('saveresponse').innerHTML = xhr.responseText;
                else
                    alert("Ajax error: No data received");
            }else alert("Ajax error: " + this.status);
        }
    };
    xhr.send(data);
}

php端:

session_start();
$mysql = new Mysql();
$data = json_decode($_POST['stringData']);
echo 'Data: ' . $data . "<br />";

通常,当我回显数据是返回Array,这是我想要的,但它不回任何东西,当我登录到这一个特定的帐户,发送的数据只是一个字符串,其中stringData是一个JSON。有没有一种方法可以查看是否有任何东西存储在那里?如果什么都没有发送,为什么会这样?对我的代码有什么建议吗?

通过添加一个随机参数来确保请求不会被缓存:

querystring = 'validate=' + validate+ "&rand=" + new Date().getTime();