Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript XMLHttpRequest发送本地数据两次_Javascript_Firefox_Firefox Addon - Fatal编程技术网

Javascript XMLHttpRequest发送本地数据两次

Javascript XMLHttpRequest发送本地数据两次,javascript,firefox,firefox-addon,Javascript,Firefox,Firefox Addon,我使用XMLHttpRequest的send方法将保存在本地sqlite数据库中的数据上传到我自己的网站。在我的网站上,我正在运行phpMyAdmin。我遇到的问题是,数据被传输到服务器两次,即使本地sqlite数据库只存储数据一次。我想知道是否有人能帮我确定这个问题。我还确保异步使用XMLHttpRequest,我仍然不明白为什么会发生这种情况。任何帮助都将不胜感激。多谢各位 postData: function(count) { var surveyURL = "https://ex

我使用XMLHttpRequest的send方法将保存在本地sqlite数据库中的数据上传到我自己的网站。在我的网站上,我正在运行phpMyAdmin。我遇到的问题是,数据被传输到服务器两次,即使本地sqlite数据库只存储数据一次。我想知道是否有人能帮我确定这个问题。我还确保异步使用XMLHttpRequest,我仍然不明白为什么会发生这种情况。任何帮助都将不胜感激。多谢各位

postData: function(count) {
    var surveyURL = "https://example.com/data/logging/answer.php";
    var file = FileUtils.getFile("ProfD", ["ext.sqlite"]);
    var dbConn = Services.storage.openDatabase(file);
    var query = dbConn.createStatement("SELECT * FROM responses");
    var i = 0;
    while (query.step()) {
      let data = {
        'rowid' : query.row.rowid,
        'timestamp' : query.row.timestamp,
        'uid' : query.row.uid,
        'warning' : query.row.warning,
        'ignored' : query.row.ignored,
        'domain' : query.row.domain,
        'qid' : query.row.qid,
        'response' : query.row.response
        };

        let xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
        xmlhttp.open("POST", surveyURL, true);
        xmlhttp.timeout = 5000;
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                if (/^0: Survey \d+ added/.test(xmlhttp.responseText)) {
                    //data was added successfully, delete the row id from local database
                        let matches = xmlhttp.responseText.match(/^0: Survey \d+ added \((\d+)\)/);
                        let rowid = matches[1];
                    ext.Debug.dump("Deleting row " + rowid);
                    try {
                        //commented this line out, because at first I thought this was the issue but itsn't?!
                        //dbConn.executeSimpleSQL("DELETE FROM responses WHERE rowid=" + rowid);
                    } catch(e) {
                        ext.Debug.dump("Error deleting row: " + e);
                    }
                } else {
                    ext.Debug.dump("Remote error: " + xmlhttp.responseText);
                }
            }
        }
            try {
            xmlhttp.send(ext.js.toString(data));
            } catch (e) {
            ext.Debug.dump("Error transmitting results: " + e);
            }
    query.reset();
  },

  testConnection: function() {
    //checks whether we can reach our server
    //only test the connection if we have stuff to upload
    //do this asynchronously
    var file = FileUtils.getFile("ProfD", ["ext.sqlite"]);
        var dbConn = Services.storage.openDatabase(file);
        var query=dbConn.createStatement("SELECT count(*) FROM responses");
        query.executeAsync({
            handleResult: function(aResultSet) {
                let count = aResultSet.getNextRow().getResultByIndex(0);
                ext.Debug.dump(count + " records to upload");
                if (count>0) {
                    var testURL = "https://example.com/data/connected.php";
                    var xmlhttp = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
                    xmlhttp.open("POST", testURL, true);
                    xmlhttp.timeout = 5000;
                    var date = Date.now();
                    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                    xmlhttp.onreadystatechange=function() {
                        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                            if (xmlhttp.responseText == date) {
                                ext.js.postData(count);
                                ext.Debug.dump("connected!");
                            } else {
                                ext.Debug.dump("not connected");
                            }
                        } else {
                            ext.Debug.dump("not connected");
                        }
                    }
                    xmlhttp.ontimeout=function() {
                        ext.Debug.dump("not connected! 3");
                    }

                    try {
                        xmlhttp.send("time=" + date);
                    } catch (e) {
                        ext.Debug.dump("Error connecting: " + e);
                    }
            }

            },

            handleError: function(aError) {
                ext.Debug.dump("Error: " + aError.message);
            },          

        });
        dbConn.asyncClose();    
  },