Jquery Ajax call and Pylons request interaction

178 views
Skip to first unread message

ravi

unread,
Jul 6, 2012, 6:33:48 AM7/6/12
to pylons-discuss
Hi,

I need help in the following issue.


JavaScript AJAX Call:
*****************************************************************
$.ajax({
type: "POST",
url: mw.config.apiBase +
"export_csv",
contentType: "application/json;
charset=utf-8",
data: {'obj':'test data'},<This can
be huge json data i want to pass>
dataType: "json",
success: function(response) {
alert(response)
}
});
******************************************************************

Python code snippet:
********************************************************************
# General Imports
import csv, sys, os, tempfile
from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect
from mapfish.protocol import create_default_filter
from mapfish.decorators import MapFishEncoder, geojsonify, _jsonify

# Local imports
from myworldapp.lib.simple_controller import SimpleController
from myworldapp.model.meta import Session
from myworldapp.lib.myw_authorizer import MywAuthorizer

class ExportCsvController( SimpleController ):

readonly = False # if set to True, only GET is supported

def __init__(self):

SimpleController.__init__( self )

#
# Build an authorizer to authorize transactions.
#
self.authorizer = MywAuthorizer( session, False )

@geojsonify
def index( self):
#
# GET /: return all features.
#
if not self.authorizer.isRequestAuthorized( request ):
abort( 403)
<How to read data sent from the AJAX>
********************************************************************

Now I want read the sent data(data: {'obj':'test data'}) from AJAX in
index method of python code

Please help me in resolving the issue

Thanks in advance

appetito

unread,
Jul 6, 2012, 10:51:59 AM7/6/12
to pylons-...@googlegroups.com
if in jQuery ajax:  
 data: JSON.stringify(your_data)

then in controller:
  request.json_body - your_data deserialized into python

Jonathan Vanasco

unread,
Jul 6, 2012, 12:24:29 PM7/6/12
to pylons-discuss
they'd also be in:

request.params
request.GET (if get)
request.POST (if post)

so you could do:

import simplejson as json
data = json.loads( request.params.get('data') )

ravi

unread,
Jul 10, 2012, 5:39:12 AM7/10/12
to pylons-...@googlegroups.com

Thank you so much appetito and Jonathan Vanasco. I resolved the issue with your suggestions.

Actually i am new to pylon and slowly i am learning it. 

Could you please suggest me the best book to learn the pylon from basics

once again thank you very much guys 
Message has been deleted

Jonathan Vanasco

unread,
Jul 10, 2012, 11:51:30 AM7/10/12
to pylons-discuss
i would not learn pylons, and learn pyramid instead.

pylons is stable , but development has ceased except for security
fixes.

pyramid is essentially the new version of pylons ( and the new version
of bfg ).

there are a lot of pyramid turorials.
Reply all
Reply to author
Forward
0 new messages