【发布时间】:2014-10-11 00:36:26
【问题描述】:
我对 python 有点陌生,并试图理解一个简单的 JSON POST 示例(我正在使用 AngularJS)。我已经阅读了其他关于 SO 的 Q 并且我看到大多数人使用某种类型的框架(即,cherrypy)。
我能够通过 POST 发送数据(字符串),并且在成功后,将其显示在一个简单的警报中。但前提是我将字符串包装在 python 代码本身的 html 标记中。但是,当我从 python 脚本中取出 HTML 标记并尝试返回 JSON 对象时,我收到 500 服务器错误。所以我的直觉告诉我,我必须使用 Web 框架?
失败的 JSON 尝试的代码。
JS:
tableModule.factory('Service', ['$http',
function($http) {
return {
post : function($path, $data, callback) {
return $http.post($path, $data,{ headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} } ).success(callback);
}
...
Service.post('test.cgi', $.param($data), function(data, status){...
而且python很简单:
import cgi
import cgitb
import sys
import traceback
import json
cgitb.enable()
def getData1():
...generate json data...
json_string = json.dumps(dictResult)
return json_string
if __name__ == "__main__":
try:
#htmlTop() <-- code not shown, when I wrap the python in html, i can return simple strings
getData1()
#htmlBottom()
except:
cgi.print_exception()
有没有办法让这个简单的例子在不安装框架的情况下工作?
【问题讨论】:
标签: javascript python json angularjs