-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathajax_json.py
54 lines (42 loc) · 1.37 KB
/
ajax_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
# SOURCE: https://stackoverflow.com/a/3753314/5909792
import cherrypy
class Root(object):
@cherrypy.expose
@cherrypy.tools.json_in()
def update(self):
input_json = cherrypy.request.json
print("input_json:", input_json)
# do_something_with(input_json)
return "Updated %r." % (input_json,)
@cherrypy.expose
def index(self):
return """
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<script type='text/javascript'>
function Update() {
$.ajax({
type: 'POST',
url: "update",
contentType: "application/json",
processData: false,
data: $('#update_box').val(),
success: function(data) {alert(data);},
error: function(data) {alert('Error:' + data);},
dataType: "text"
});
}
</script>
<input type='textbox' id='update_box' value='{"a": 2, "b": 3}' size='100' />
<input type='submit' value='Update' onClick='Update(); return false' />
</body>
</html>
"""
if __name__ == "__main__":
cherrypy.quickstart(Root())