-2

I would like to send a list of dictionaries from a web browser to a server running CherryPy. What should the code in CherryPy be to receive it?

JavaScript in Web Browser

var listOfDictionaries = {
    "Coordinates":
    [
        {"x":"892850686394369 ","y":"4c189d55d5a2b4d682647bfcc9e5827112abfe7c"},
        {"x":"892850686394430 ","y":"b1c8238337a3e17352718a46ca0a76a7e196adfd"}
    ]
};

$.post('drawChart', listOfDictionaries,
    function (data) {
        $("#title").html(data['title']);
    });

Data that I am sending via an HTTP POST:

{"Coordinates":[{"x":"892850686394369 ","y":"4c189d55d5a2b4d682647bfcc9e5827112abfe7c"},{"x":"892850686394430 ","y":"b1c8238337a3e17352718a46ca0a76a7e196adfd"}]}

CherryPy method on Web Server

@cherrypy.expose
def drawChart(self, x, y):
    cherrypy.response.headers['Content-Type'] = 'application/json'
    return json.dumps(dict(title="x: %s" x))
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
Rokas.ma
  • 191
  • 1
  • 12
  • What d you mean receive it? Receive it where? The browser? – kylieCatt Jul 15 '15 at 13:41
  • @IanAuld I am sending list of dictionaries via post method from client side (browser) and the question is that how should function look in server side (cherrypy) to receive data (list of dictionaries) – Rokas.ma Jul 15 '15 at 13:45
  • possible duplicate of [How to receive JSON in a POST request in CherryPy?](http://stackoverflow.com/questions/3743769/how-to-receive-json-in-a-post-request-in-cherrypy) – kylieCatt Jul 15 '15 at 13:50
  • @IanAuld I looked there before posting, but there is not what I need, I did not find solution there – Rokas.ma Jul 15 '15 at 13:57
  • http://cherrypy.readthedocs.org/en/latest/basics.html#dealing-with-json – kylieCatt Jul 15 '15 at 14:03
  • and what do u want to say with that link? It does not help – Rokas.ma Jul 15 '15 at 16:43
  • 1
    That's the documentation for the framework you are using on how to accept JSON data from POST requests. It's highly relevant to your question and in fact shows you exactly what you need to do. – kylieCatt Jul 15 '15 at 16:46
  • You shouldn't ask the [same question](http://stackoverflow.com/questions/31427742/send-back-json-to-client-side) over again. – saaj Jul 17 '15 at 14:06

1 Answers1

0

Sorry for late answer, I solved this problem like this:

client side (browser):

    coordinates = {'coordinates': coordinates}
    $.post('/drawChart', coordinates, function (data) {
                    createGraph((data['Coordinates']));
                });

server side (python):

@cherrypy.expose
def drawChart(self, coordinates):
    cherrypy.response.headers['Content-Type'] = 'application/json'
    listOfCoordiantes = randomData(coordinates)
    return json.dumps(dict(Coordinates=listOfCoordiantes))
Rokas.ma
  • 191
  • 1
  • 12