Python 将json发送回客户端
我刚开始用cherrypy开发,所以我有点挣扎。 在客户端,我选择一些数据,将其转换为json并通过post方法发送到服务器端。然后我用json做了一些操作,最后我想把它发送回客户端。因此,问题是如何将修改后的json返回到客户端浏览器 服务器端:Python 将json发送回客户端,python,json,web-services,post,cherrypy,Python,Json,Web Services,Post,Cherrypy,我刚开始用cherrypy开发,所以我有点挣扎。 在客户端,我选择一些数据,将其转换为json并通过post方法发送到服务器端。然后我用json做了一些操作,最后我想把它发送回客户端。因此,问题是如何将修改后的json返回到客户端浏览器 服务器端: @cherrypy.expose def drawChart(self): __test = cherrypy.request.body.read().strip() logging.debug(__test)
@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
});
}
只要换成这一行,而不是你所拥有的
success: drawChart,
编辑:
哦-并将您的处理程序更改为:
def drawChart(self, data):
希望这有帮助 使用
我的做法是:
@cherrypy.expose
def drawChart(self, coordinates):
cherrypy.response.headers['Content-Type'] = 'application/json'
listOfCoordiantes = randomData(coordinates)
return json.dumps(dict(Coordinates=listOfCoordiantes))
如果我像这样发送数据{x:111}是可以的,但是如果我像这样发送json:{x:892850686394369,y:4c189d55d5a2b4d682647fcc9e5827112abfe7c},{x:89285068639430,y:b1c8238337a3e17352718a46ca0a76a7e196adfd}我的方法在python中应该是什么样子?@Rokas.ma您可以双向传递任意json数据。以cherrypy.request.json为例,执行转换,从处理程序返回结果。
@cherrypy.expose
def drawChart(self, coordinates):
cherrypy.response.headers['Content-Type'] = 'application/json'
listOfCoordiantes = randomData(coordinates)
return json.dumps(dict(Coordinates=listOfCoordiantes))