如何使用cherrypy进行async ajax调用?

时间:2021-12-30 20:59:05

I'm using cherrypy's standalone server (cherrypy.quickstart()) and sqlite3 for a database.

我正在使用cherrypy的独立服务器(cherrypy.quickstart())和sqlite3作为数据库。

I was wondering how one would do ajax/jquery asynchronous calls to the database while using cherrypy?

我想知道如何在使用cherrypy时对数据库进行ajax / jquery异步调用?

2 个解决方案

#1


2  

The same way you would do them using any other webserver - by getting your javascript to call a URL which is handled by the server-side application.

使用任何其他网络服务器的方式相同 - 通过让您的javascript调用由服务器端应用程序处理的URL。

#2


9  

If you are using CherryPy 3.2.0-rc1 then you can use the decorators @json_in and @json_out (see here).

如果你使用的是CherryPy 3.2.0-rc1,那么你可以使用装饰器@json_in和@json_out(见这里)。

Thus:

从而:

@cherrypy.expose
@tools.json_in(on = True)
@tools.json_out(on = True)
def json_test(self):
    return { 'message':'Hello, world!' }

will return JSON to the browser, e.g.

将JSON返回给浏览器,例如

$(document).ready(function() {
    $.getJSON('/json_test', function(data) {
        alert(data.message);
    }
}

You need to remember that CherryPy expects JSON posts to have a content type of application/json, to do that with jQuery, either use $.ajax and manaully set contentType or you can use the following convenience function:

你需要记住,CherryPy希望JSON帖子有一个application / json的内容类型,用jQuery做,使用$ .ajax和manaully设置contentType,或者你可以使用以下便利功能:

$.postJSON = function(url, data, callback) {
    $.ajaxSetup({ scriptCharset:"utf-8", 
                    contentType:"application/json; charset=utf-8" });
    $.post(url, $.toJSON(data), callback, "json");
}

This function uses the jquery-json plugin, but you could use a different method to convert to JSON.

此函数使用jquery-json插件,但您可以使用其他方法转换为JSON。

#1


2  

The same way you would do them using any other webserver - by getting your javascript to call a URL which is handled by the server-side application.

使用任何其他网络服务器的方式相同 - 通过让您的javascript调用由服务器端应用程序处理的URL。

#2


9  

If you are using CherryPy 3.2.0-rc1 then you can use the decorators @json_in and @json_out (see here).

如果你使用的是CherryPy 3.2.0-rc1,那么你可以使用装饰器@json_in和@json_out(见这里)。

Thus:

从而:

@cherrypy.expose
@tools.json_in(on = True)
@tools.json_out(on = True)
def json_test(self):
    return { 'message':'Hello, world!' }

will return JSON to the browser, e.g.

将JSON返回给浏览器,例如

$(document).ready(function() {
    $.getJSON('/json_test', function(data) {
        alert(data.message);
    }
}

You need to remember that CherryPy expects JSON posts to have a content type of application/json, to do that with jQuery, either use $.ajax and manaully set contentType or you can use the following convenience function:

你需要记住,CherryPy希望JSON帖子有一个application / json的内容类型,用jQuery做,使用$ .ajax和manaully设置contentType,或者你可以使用以下便利功能:

$.postJSON = function(url, data, callback) {
    $.ajaxSetup({ scriptCharset:"utf-8", 
                    contentType:"application/json; charset=utf-8" });
    $.post(url, $.toJSON(data), callback, "json");
}

This function uses the jquery-json plugin, but you could use a different method to convert to JSON.

此函数使用jquery-json插件,但您可以使用其他方法转换为JSON。