Vineet
Vineet

Reputation: 624

$.ajax function : send json data : parse at serverside function

I am using $.ajax function to send json data to serverside function.

var jsonObjects = [{id:1, name:"amit"}, {id:2, name:"ankit"},{id:3,name:"atin"},{id:1, name:"puneet"}];

$.ajax({
     url: "{{=URL('myControllerName')}}",
     type: "POST",
     context: document.body,
     data: {students: JSON.stringify(jsonObjects) },
     dataType: "json",
     success: function(){
         alert('ok');
  }

});

In the serverside function, how do I access the data?

Somebody has give the code for grails as :---

//this code is written in grails
import grails.converters.JSON;
List<JSON> students = JSON.parse(params.students) //students in request params is parsed to json objects and stored in the List
println "Student id: " + students[0].studentId    //first element of the students list is accessed as a map holding a key studentId

I want to do this in a python web framework viz. web2py.

Tried to access it as params.students and request.students, but no luck.

What is the correct syntax to access the data sent? (I checked the jQuery API, but couldn't find the same).

Thanks,

Vineet

Upvotes: 3

Views: 1184

Answers (1)

Marcin
Marcin

Reputation: 49856

You are confused.

"How to access the data on the serverside" has nothing to do with jquery, and everything to do with your server-side web framework.

You need to extract the data from the request object; the web2py documentation for that is here: http://web2py.com/book/default/chapter/04#request

Upvotes: 4

Related Questions