JSON数据日志并发送到服务器

时间:2014-08-21 10:24:12

标签: javascript json

我有一个javascript函数,它每隔一定时间生成一次JSON数据,然后将它放到云服务器上。现在我不想实时发布POST,而是想将这些数据记录在一个缓冲区中,然后在n个数据日志之后说我将PUT转为云。例如,我想在10秒内记录50个数据点,然后使用时间戳我将PUT发送到服务器

现在JSON数据通过var fromDatan传递。 JSON数据格式是

{"value":"-2.1282838391939194"}

现在代码是:

var acc;
    var watchID = null;

function startWatch() {

            //set frequency of accelerometer update.1000 = 1 second

            var options = { frequency: 1000 };
            watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);

        }


    function stopWatch() {
            if (watchID) {
            navigator.accelerometer.clearWatch(watchID);
            watchID = null;
            }
        }


    //DISPLAY ACCELERATION DATA

    function onSuccess(acceleration) {
            acc = acceleration;
            var element = document.getElementById("accelerometer");
            element.innerHTML = 'Acceleration X: ' + acceleration.x         + '<br />' +
                              'Acceleration Y: ' + acceleration.y         + '<br />' +
                              'Acceleration Z: ' + acceleration.z         + '<br />' +
                              'Timestamp: '      + acceleration.timestamp + '<br />';

                datalock();


        }



    function postdata () {
        var accx = acc.x;
        alert(accx);




            var fromDatan = JSON.stringify(fromData);
                alert(fromDatan);

                //POST JSON  DATA

                $.ajax({
                url: "http://abcd.com",
                headers: {
                    "X-API-KEY": "2b9asdedqedqxdqd7956e6f7a",
                    "Content-Type": "application/json"
                },
                type: "PUT",
                data: fromDatan,
                dataType: "JSON",
                success: function(fromData, status, jqXHR) {
                    alert(JSON.stringify(fromData));
                },

                error: function(jqXHR, status) {
                    alert(JSON.stringify(jqXHR));
                }
                });
                return false;

        }

        function datalock(){
            alert("Entering");
            fromData.push({
                value: accx.toString(),
            });
            if (fromData.length >= 10) {
                postdata ();
            };

        var fromData;

        }

它不起作用,虽然所有其他事情都是正确的,推动我无法正确实施。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

编辑开始

试试这个:

var fromData = [];
var jsonCounter = 0;

function postdata () {
    var accx = acc.x;
    alert(accx);

    var fromDatan = JSON.stringify(fromData);
    alert(fromDatan);

    $.ajax({
    url: "http://abcd.com",
    headers: {
        "X-API-KEY": "2b9asdedqedqxdqd7956e6f7a",
        "Content-Type": "application/json"
    },
    type: "PUT",
    data: fromDatan,
    dataType: "JSON",
    success: function(fromData, status, jqXHR) {
        alert(JSON.stringify(fromData));
    },

    error: function(jqXHR, status) {
        alert(JSON.stringify(jqXHR));
    }
    });
    return false;
}

function datalock(){
    alert("Entering");
    fromData.push({
        value: accx.toString(),
    });

    jsonCounter++;

    // post only after 10 entries
    if (jsonCounter >= 10) {
    postdata ();
    jsonCounter = 0; // reset
    };
}

编辑结束

由于JSON对象没有length属性,您可以使用变量来完成。

示例:

var formdata;
var jsonCounter = 0;

function datalock(){
    alert("Entering");
    fromData.push({
        value: accx.toString(),
    });

    jsonCounter++;

    // post only after 50 entries
    if (jsonCounter >= 50) {
        postdata ();
        jsonCounter = 0; // reset
    };

    //var fromData; // make it global as it is used in other function.
}

答案 1 :(得分:0)

根据@ web-nomad

的建议修改后,这是我的代码
    function postdata () {
            var accx = acc.x;
            alert(accx);




                var fromDatan = JSON.stringify(fromData);
                    alert(fromDatan);

                    //POST JSON SENSOR DATA

                    $.ajax({
                    url: "http://abcd.com",
                    headers: {
                        "X-API-KEY": "2b9e259asdasd6a7956e6f7a",
                        "Content-Type": "application/json"
                    },
                    type: "PUT",
                    data: fromDatan,
                    dataType: "JSON",
                    success: function(fromData, status, jqXHR) {
                        alert(JSON.stringify(fromData));
                    },

                    error: function(jqXHR, status) {
                        alert(JSON.stringify(jqXHR));
                    }
                    });
                    return false;

            //var fromData = {};
            }

            var fromData = [];
            var jsonCounter = 0;

//Edit start

            function datalock() {
            alert("Entering");
            //alert(jsonCounter); //It was showing me only count as 0
            fromData.push({
            value: accx.toString(),
            });
            alert(JSON.stringify(fromData));// no alert
            jsonCounter++;
            alert(jsonCounter); //The alert was not coming at all, when I tried this one
            // post only after 10 entries
            if (jsonCounter >= 10) {
            postdata ();
            jsonCounter = 0; // reset
            };
        }

//Edit End

这是在您给出@ web-nomad

的建议后的更改