send json variable as parameters to RESTful api

28 views
Skip to first unread message

chuan137

unread,
Jun 16, 2014, 5:17:57 PM6/16/14
to web...@googlegroups.com
Dear all,

In my javascript code, I would like to have sth like this,

$.getJSON(url_to_REST_api, {
            group: 0,
            sensors: sensorIDs
        }, function(res) {
            timestamp = res['timestamp'];
            data = res['data'];
        });

However, sensorIDs is a rather long array, which in turn generate a request url very ugly, like this
http://localhost:8000/service/test/katrin?group=0&sensors%5B%5D=2&sensors%5B%5D=3&sensors%5B%5D=4&sensors%5B%5D=5&sensors%5B%5D=6&sensors%5B%5D=7&sensors%5B%5D=8&sensors%5B%5D=9&sensors%5B%5D=74&sensors%5B%5D=66&sensors%5B%5D=68&sensors%5B%5D=99&sensors%5B%5D=75&sensors%5B%5D=67&sensors%5B%5D=69&sensors%5B%5D=100&sensors%5B%5D=102&sensors%5B%5D=106&sensors%5B%5D=107&sensors%5B%5D=108&sensors%5B%5D=149&sensors%5B%5D=150&sensors%5B%5D=103&sensors%5B%5D=109&sensors%5B%5D=110&sensors%5B%5D=111&sensors%5B%5D=151&sensors%5B%5D=152&sensors%5B%5D=59&sensors%5B%5D=61&sensors%5B%5D=75&sensors%5B%5D=78&sensors%5B%5D=80&sensors%5B%5D=82&sensors%5B%5D=145&sensors%5B%5D=112&sensors%5B%5D=113&sensors%5B%5D=116&sensors%5B%5D=117&sensors%5B%5D=118&sensors%5B%5D=146&sensors%5B%5D=119&sensors%5B%5D=120&sensors%5B%5D=123&sensors%5B%5D=124&sensors%5B%5D=125&sensors%5B%5D=186&sensors%5B%5D=187&sensors%5B%5D=188&sensors%5B%5D=190&sensors%5B%5D=191&sensors%5B%5D=192&sensors%5B%5D=180&sensors%5B%5D=181&sensors%5B%5D=182&sensors%5B%5D=183&sensors%5B%5D=184&sensors%5B%5D=185

What best strategy should I take? Change the $.getJSON() to Post and Get? (I am not sure how to do it yet) And how to design my api?

Thanks in advance.

Chuan 

Massimo Di Pierro

unread,
Jun 19, 2014, 6:22:26 PM6/19/14
to web...@googlegroups.com
You can do:

jQuery.ajax({
                    type: "POST",
                        url: url,
                        data: JSON.stringify(data),
                        success: callback,
                        dataType: "json",
                        contentType: APPLICATION_JSON,
                        processData: false
                        }).then(......);
Reply all
Reply to author
Forward
0 new messages