-
4 Jul 2008 12:34 PM #1
posting JSON data with ext.ajax.request
posting JSON data with ext.ajax.request
i am trying to use the following code to post some json data to a php script:
i can see the POST being made in firebug, but my question is, how do i retrieve the posted data on the PHP side? i know i can access $_POST['ajax_req'], but where does jsonData go?Code:Ext.Ajax.request({ url: session_manager_url, method: 'POST', jsonData: json, params: { ajax_req: 'set_session' }, success: function(transport){ // do something }, failure: function(transport){ alert("Error: " - transport.responseText); } });
am i using the wrong method for what i am trying to accomplish?
-
4 Jul 2008 1:38 PM #2Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- Frederick MD, NYC, DC
- Posts
- 16,170
- Vote Rating
- 33
i would do it like this, so you have full control:
... /on phpCode:var myObj = { param1 : 'something', obj1 : { a : 'something in obj1' } } Ext.Ajax.request({ url: session_manager_url, method: 'POST', params: { ajax_req: Ext.util.JSON.encode(myObj) }, success: function(transport){ // do something }, failure: function(transport){ alert("Error: " - transport.responseText); } });
Code:<? $myObj = json_decode(strip_slashes($_REQUEST['ajax_req'])); print $myObj->param1; print $myObj->obj1->a; ?>

Jay Garcia @ModusJesus || Modus Create co-founder
Ext JS in Action author
Sencha Touch in Action author
Get in touch for Ext JS & Sencha Touch Touch Training
We are also working on Video-based Sencha Touch training: Check it out here.
-
4 Jul 2008 1:41 PM #3Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- Frederick MD, NYC, DC
- Posts
- 16,170
- Vote Rating
- 33
fyi:
# jsonData : Object/String (Optional)
JSON data to use as the post. Note: This will be used instead of params for the post data. Any params will be appended to the URL.
Jay Garcia @ModusJesus || Modus Create co-founder
Ext JS in Action author
Sencha Touch in Action author
Get in touch for Ext JS & Sencha Touch Touch Training
We are also working on Video-based Sencha Touch training: Check it out here.
-
4 Jul 2008 2:23 PM #4
thanks for your response.
it was the encode part that i was missing .. duh! the data being passed to the function sending the request should have been json encoded, or so i assumed.
i had the code like you suggested to begin with, but changed it to use the jsonData parameter because it wasn't working.
-
10 Jul 2008 9:38 AM #5
yes, I have the same question.
and I use:
instead ofCode:jsondata: myJsondata
because the jsondata is not recongized, it shows: [object, object]Code:params{ action: save, jsondata: myJsondata }
By the way, if the server side is jsp, you need to read it out from the request.getInputStream()good good study,
day day up ; )
-
10 Jul 2008 9:51 AM #6
it may be a typo in your post, but you have shown use of jsondata instead of jsonData.
to be honest, i dont see the utility in using jsonData. cant the jsonObject always be passed as params?
-
10 Jul 2008 11:27 AM #7
The two methods will result in different POST bodies.
Using jsonData, the actual JSON text is passed in the POST body.
Using params, the JSON text is encoded into one of the HTTP parameters in the POST body.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
10 Jul 2008 11:36 AM #8
I think theres sill some confusion..
I am curious about what is the difference here?
jsonData: someObject
vs
params: someObject
-
10 Jul 2008 1:45 PM #9
using jsonData serializes an object into "proper" JSON. JSON is a textual representation ot a Javascript object. The HTTP packet contains a javascript literal.
Passing it in params means that the HTTP packlet contains eg:
No HTTP Parameters in the first version.Code:param1=foo¶m2=bar&myJson={foo:'bar'}
The second version is standard HTTP parameters.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
10 Jul 2008 1:53 PM #10
ahh i see it now.
seems like it would be of limited use right now in most cases (except maybe for java based backends?). I could see the benefit of passing json in the post body and having it be automatically turned into an object in php, rather than dealing with POST/GET vars tho. might hafta look into that


Reply With Quote