3

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

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Vineet
  • 624
  • 1
  • 11
  • 26
  • Python 2.6 has a `json` module you can use for parsing JSON (see [How to decode JSON with Python](http://stackoverflow.com/q/2331943/218196)). As for how web2py handles request, have a look at its documentation. – Felix Kling Feb 21 '12 at 11:05
  • I imported the json module and tried to parse the data. No JSON error. But the error thrown because data is not assigned correctly (in params.students, params is not defined). – Vineet Feb 21 '12 at 11:10
  • 1
    That's why I said you have to read about web2py, how they make the request data available. You cannot just copy and paste code from a different language. Obviously web2py does not provide a `params` variable. I'm pretty sure this is covered by some tutorial. – Felix Kling Feb 21 '12 at 11:12
  • In web2py, for a url like www.myshineyurl.com/1/2 , we parse it like request.args(0) and request.args(1) which give values 1 and 2 resp. For a builtin function viz. ajax(), we access it as request.vars.htmlElementName. But there isn't any info for accessing data from $.ajax function. That's why I posted the question here. – Vineet Feb 21 '12 at 11:17
  • 1
    It's a POST request. It does not matter whether it is send via Ajax or not. Read about how to access data from POST requests. – Felix Kling Feb 21 '12 at 11:18
  • Yes! You are right. After accessing the data in POST request as request.vars.students, it worked. Thanks for your inputs :) – Vineet Feb 21 '12 at 11:27

1 Answers1

4

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

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • Great! request.vars.students worked. I didn't know that. Thank you very much !! I have accepted this answer :-) – Vineet Feb 21 '12 at 11:25
  • @Vineet: no problem! I'm always happy to help people learn how to solve their own problems. – Marcin Feb 21 '12 at 11:39