Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$.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

like image 298
Vineet Avatar asked Dec 05 '25 11:12

Vineet


1 Answers

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

like image 186
Marcin Avatar answered Dec 07 '25 00:12

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!