English 中文(简体)
How to pass a JSON object to web2py using JQuery Ajax
原标题:

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 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()
问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签