javascript - 使用angularjs将json对象传递给python

标签 javascript python json angularjs

我是 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 的错误用法吗?任何帮助将不胜感激! :)

最佳答案

根据 JSONDecodeErrorjsonObj 变量不包含有效的 JSON 对象。

我相信问题出在这里:

jsonObj = this.form.toJson;

您应该调用 toJson 方法而不是将其分配给变量:

jsonObj = angular.toJson(this.form);

关于javascript - 使用angularjs将json对象传递给python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9786781/

相关文章:

javascript - 尝试在 onclick 中定义一个函数

Python 和 Jinja2 - 导入另一个环境的模板

Python:如何安装传递复制/深度复制 Hook

php - 将 MySQL 数据编码为 JSON

javascript - 如何使用变量作为 json 属性?

javascript - Flow Router 获取当前 Router 服务器端

javascript - 每个带有 ajax 调用的 jQuery 将在完成之前继续

javascript - 我们在 SharePoint 中使用的 javascript 窗口解决方案的 jquery 弹出替代方案

python - 我在切片范围内使用什么值来包含 numpy 数组中的最后一个值?

json - log4net.Ext.Json 自定义属性未出现在 json 中