发回json到客户端 [英] Send back json to client side
本文介绍了发回json到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚开始使用cherrypy进行开发,所以我有点挣扎. 在客户端,我正在选择一些数据,将其转换为json并通过post方法发送到服务器端.然后,我对json进行了一些操作,最后我想将其发送回客户端.因此,问题是如何将修改后的json返回给客户端(浏览器).
I just started developing with cherrypy, so I am struggling a little bit. In client side I am selecting some data, converting it to json and sending to server side via post method. Then I am doing a few operations with json and finally I want to send it back to client side. So the question is how to return modified json to the client side (browser).
服务器端:
@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
});
}
推荐答案
我的方法是:
@cherrypy.expose
def drawChart(self, coordinates):
cherrypy.response.headers['Content-Type'] = 'application/json'
listOfCoordiantes = randomData(coordinates)
return json.dumps(dict(Coordinates=listOfCoordiantes))
这篇关于发回json到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文