i am trying to use the following code to post some json data to a php script:
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);
}
});
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?
am i using the wrong method for what i am trying to accomplish?
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.
ahh i see it now.
seems like it would be of limited use right now in most cases (except maybe for java based backends?).
While jsonData will save some lines of code, i like having full control over the var labels being passed. Eg:
http://tdg-i.com/img/screencasts/2008-07-10_1842.swf
Where my submit code looks like:
submit : function() {
if (this.isValid(true)) {
this.el.mask('saving data...', 'x-mask-loading');
var data = Ext.util.JSON.encode(this.getValues()); Javascript pastebin - collaborative debugging tool:: Ext.Ajax.request({. waitMsg: 'Please wait',. url: '/invoice/update?format= json',. params: {. update: true,. id: oGrid_event.record.data.inv_id, http://pastebin.com/m20303a2aHOME | phpied.com ยป json:: An AJAX request fired from your beloved clean external .js that will only return the JSON data you hacked into your island thing of yours? Same as above. http://www.phpied.com/category/json/HOME |
Ext.Ajax.request({
url : 'vendorDbManager.php',
method : 'POST',
params : {
values : data
},
scope : this,
callback : function(options, success, response) {
if (success) {
this.el.unmask();
Ext.MessageBox.alert('Success!', 'Your data has been saved');
}
}
});
}
},
just to show myself how it worked, i made a simple proof of concept:
function(){
Ext.Ajax.request({
url: 'test.php',
method: 'POST',
jsonData: {
param1: 'test',
param2: [1,2,3]
}
})
}
and
if ($HTTP_RAW_POST_DATA) {
$request = json_decode($HTTP_RAW_POST_DATA);
print_r($request);
}
which spat back:
stdClass Object ( [param1] => test [param2] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
pretty cool stuff, even if it just presents the same data object on the backend with a few less decoding steps :)
perhaps might make sanitizing a little easier, having just one string to work with.
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.
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:
param1=foo¶m2=bar&myJson={foo:'bar'}
No HTTP Parameters in the first version.
The second version is standard HTTP parameters.
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 :)
yes, I have the same question.
and I use:
jsondata: myJsondata
instead of
params{
action: save,
jsondata: myJsondata
}
because the jsondata is not recongized, it shows: [object, object]
By the way, if the server side is jsp, you need to read it out from the request.getInputStream()
i would do it like this, so you have full control:
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);
}
});
... /on php
$myObj = json_decode(strip_slashes($_REQUEST['ajax_req']));
print $myObj->param1;
print $myObj->obj1->a;
?>
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?
I think theres sill some confusion..
I am curious about what is the difference here?
jsonData: someObject
vs
params: someObject
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.
How much does getting a small tattoo on your hip/stomach hurt?
Do anyone else have an itchy anus? ?
|