【问题标题】:Send back json to client side将 json 发送回客户端
【发布时间】:2015-10-04 08:04:52
【问题描述】:

我刚开始用cherrypy开发,所以我有点挣扎。 在客户端,我正在选择一些数据,将其转换为 json 并通过 post 方法发送到服务器端。然后我用json做一些操作,最后我想把它发回客户端。所以问题是如何将修改后的 json 返回到客户端(浏览器)。

服务器端:

  @cherrypy.expose
  def drawChart(self):
        __test = cherrypy.request.body.read().strip()
        logging.debug(__test)
        #...some operations with data

客户端:

function send(JsonArray){
        $.ajax({
            url: '/drawChart',
            type: 'post',
            contentType: 'application/json',
            dataType: 'json',
            success: console.log("Success!"),
            data: JsonArray
        });
}

【问题讨论】:

    标签: python json web-services post cherrypy


    【解决方案1】:

    只需更改为这一行,而不是你所拥有的...

                success: drawChart,
    

    编辑: 哦 - 并将您的处理程序更改为:

    def drawChart(self, data):
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      使用CherryPy JSON tools

      #!/usr/bin/env python
      # -*- coding: utf-8 -*-
      
      
      import cherrypy
      
      
      config = {
        'global' : {
          'server.socket_host' : '127.0.0.1',
          'server.socket_port' : 8080,
          'server.thread_pool' : 8
        }
      }
      
      class App:
      
        @cherrypy.expose
        def index(self):
          return '''<!DOCTYPE html>
            <html>
            <head>
              <title>CherryPy demo</title>
              <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
              <script type='text/javascript'>
                $.ajax({
                  'type'        : 'POST',
                  'dataType'    : 'JSON',
                  'contentType' : 'application/json',
                  'url'         : '/someendpoint',
                  'data'        : JSON.stringify({'foo': ['bar']}),
                  'success'     : function(response)
                  {
                    console.log(response);
                  }
                });
              </script>
            </head>
            </html>
          '''
      
        @cherrypy.expose
        @cherrypy.tools.json_in()
        @cherrypy.tools.json_out()
        def someendpoint(self):
          yourDict = cherrypy.request.json
          yourDict['foo'].append('baz')
      
          return yourDict
      
      
      if __name__ == '__main__':
        cherrypy.quickstart(App(), '/', config)
      

      【讨论】:

      • 如果我发送这样的数据 {x: "111"} 是可以的,但是如果我发送这样的 json: {"Coordinates":[{"x":"892850686394369", "y":"4c189d55d5a2b4d682647bfcc9e5827112abfe7c"},{"x":"892850686394430","y":"b1c8238337a3e17352718a46ca0a76a7e196adfd"}]} 我在 python 中的方法应该怎么看?
      • @Rokas.ma 您可以双向传递任意 JSON 数据。拿cherrypy.request.json,你做转换,从你的处理程序返回结果。
      【解决方案3】:

      我的做法是:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-17
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多