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

6
  • Python 2.6 has a json module you can use for parsing JSON (see How to decode JSON with Python). As for how web2py handles request, have a look at its documentation. Commented Feb 21, 2012 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). Commented Feb 21, 2012 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. Commented Feb 21, 2012 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. Commented Feb 21, 2012 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. Commented Feb 21, 2012 at 11:18

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Great! request.vars.students worked. I didn't know that. Thank you very much !! I have accepted this answer :-)
@Vineet: no problem! I'm always happy to help people learn how to solve their own problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.