如何通过JSON对象使用JQuery Ajax来的web2py [英] How to pass a JSON object to web2py using JQuery Ajax

查看:204
本文介绍了如何通过JSON对象使用JQuery Ajax来的web2py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这在.NET方法来传递数据来回的客户端,并使用JSON对象(双向)服务器之间。我真的很喜欢的方式,我期待做的web2py类似的事情。 web2py的支持返回JSON对象,并支持jsonrpc。我没有但是能使其解析JSON对象。我的客户端调用看起来是这样的:

I've used this method in .NET to pass data back and forth between client and server using JSON objects (both ways). I really liked the method and am looking to do something similar with web2py. Web2py supports returning json objects and supports jsonrpc. I haven't however been able to make it parse a JSON object. My client call looks like this:

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行动,并没有什么作品。

I've tried a bunch of stuff in my jsontest action and nothing works.

任何人都已经能够完成类似的东西?

Has anyone been able to accomplish something similar?

许多AP preciated。

Much appreciated.

推荐答案

有多种方式。你的情况做最简单的事情是

there are multiple ways. In your case the simplest thing to do is

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

这篇关于如何通过JSON对象使用JQuery Ajax来的web2py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆