开发者

How to pass a JSON object to web2py using JQuery Ajax

开发者 https://www.devze.com 2022-12-11 00:03 出处:网络
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

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'); }
});

I've tried a开发者_StackOverflow中文版 bunch of stuff in my jsontest action and nothing works.

Has anyone been able to accomplish something similar?

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()
0

精彩评论

暂无评论...
验证码 换一张
取 消