XMLHttpRequest两次发送本地数据

时间:2015-03-22 10:07:44

标签: javascript firefox firefox-addon

我使用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();    
  },

0 个答案:

没有答案