【问题标题】:How to pass a JSON object to web2py using JQuery Ajax如何使用 JQuery Ajax 将 JSON 对象传递给 web2py
【发布时间】:2009-11-12 01:15:46
【问题描述】:

我在 .NET 中使用 this method 使用 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 操作中尝试了很多东西,但没有任何效果。

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

非常感谢。

【问题讨论】:

    标签: ajax json web2py


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-06-12
      • 2017-03-02
      • 1970-01-01
      • 2014-07-28
      • 2013-01-29
      • 2012-04-12
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      相关资源
      最近更新 更多