我是 angularjs 和 python 的新手,我遇到了这个问题。我一直在尝试使用 angularjs 将表单数据传递给 Python 服务器端。在将其发送到我的 .js Controller 之前,我已将表单转换为 json 对象。
controller.js:
jsonObj = this.form.toJson;
$xhr('POST','/form/processform',jsonObj,function() {
alert("Done!");
window.load("/");
}, function(){
"Request failed";
});
python :
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import simplejson as json
class processForm(webapp.RequestHandler):
def post(self):
form = json.loads(self.request.body)
# process forms
self.redirect("#/")#redirects to main page
我收到一个名为“JSONDecodeError:无法解码 JSON 对象”的错误。我尝试将“POST”替换为“JSON”,但似乎效果不佳。我还阅读了 angularjs 中的 $resource,但我不确定如何使用它。
这是因为 $xhr 的错误用法吗?任何帮助将不胜感激! :)
最佳答案
根据 JSONDecodeError
,jsonObj
变量不包含有效的 JSON 对象。
我相信问题出在这里:
jsonObj = this.form.toJson;
您应该调用 toJson
方法而不是将其分配给变量:
jsonObj = angular.toJson(this.form);
关于javascript - 使用angularjs将json对象传递给python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9786781/