ajax - 如何使用 JQuery Ajax 将 JSON 对象传递给 web2py

标签 ajax json web2py

我用过 this method在 .NET 中使用 JSON 对象(两种方式)在客户端和服务器之间来回传递数据。我真的很喜欢这种方法,并且希望用 web2py 做一些类似的事情。 Web2py 支持返回 json 对象并支持 jsonrpc。但是,我无法使其解析 JSON 对象。我的客户电话如下所示:

var testObject = {};
testObject.value1 = "value1value!";
testObject.value2 = "value2value!";

var DTO = { 'testObject' : testObject };
var data = $.toJSON(DTO);    //Using the toJSON plugin by Mark Gibson

  $.ajax({
    type: 'POST',
    url: '/MyWeb2PyApp/MyController/jsontest.json',
    contentType: "application/json; charset=utf-8", 
    data: data,
    dataType: 'json',
success:  function(data){  alert('yay'); }
});

我在 jsontest 操作中尝试了很多东西,但没有任何效果。

有没有人能够完成类似的事情?

非常感激。

最佳答案

有多种方式。在你的情况下,最简单的事情是

def jsontest():
   import gluon.contrib.simplejson
   data = gluon.contrib.simplejson.loads(request.body.read())
   return dict()

关于ajax - 如何使用 JQuery Ajax 将 JSON 对象传递给 web2py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1719244/

相关文章:

javascript - 使用 multer 接收 FormData 对象作为 node.js 服务器

json - 启用 JSON 以从 WSO2 数据服务检索数据

json - 将 JSON 事件聚合到 Azure 流分析中的数组中

java - php - 在数据库中存储发布的 JSON 对象

python - web2py 无法识别模块目录中的模块

python - GAE 上的 B2B 应用程序身份验证 - Google 帐户或自定义用户群(Django 或 Web2Py)?

javascript - jQuery等js使用div.load时未定义src

php - jquery中.toggle函数与.load函数结合使用

python - web2py 中的 HTML 模板解析

javascript - Ajax 响应文本区域的换行符