简体   繁体   中英

XMLHttpRequest sometimes doesn't send string

I'm not sure why this is happening but I will try to explain as much as I already know. I have a website that allows you to log in with an account stored on a database. Each user can update their own set of data that is also in the same database. When a user changes data Javascript will post an XMLHttpRequest to a php file on the server. The data is JSON encoded and is decoded in the php file and then stores the data on the database.

The problem is whenever I log into a specific account no data is sent. The string is empty after the request is sent. The post works and the php file runs but no data is present. Here is my code for sending the request in 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);

}

On the php side:

session_start();

$mysql = new Mysql();

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

Normally when I echo the data is returns Array which is what I want but it doesn't echo anything when ever I log into this one specific account, the data sent is just a string where stringData is a JSON. Is there a way to see if anything IS stored there? Also if nothing is being sent why could this be? Any suggestions for my code?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM