HZNM.COM
welcome to my space
X
Search:  
 HOME   posting JSON data with ext.ajax.request
posting JSON data with ext.ajax.request
Published by: mike 2009-01-09
Welcome to:hznm.com

  • 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/m20303a2a
    HOME
    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? ?

    You are looking at:hznm.com's posting JSON data with ext.ajax.request, click hznm.com to home
  • what type of job would fit
  • why do i always fall for the worst guys
  • can nascar driver jimmie johnson go for a 3 some
  • why has my broken big toe not healed
  • what should i get for my friends birthday it 039 s today
  • help quick before and after pill
  • whats the funniest craziest thing you 039 ve done when your drunk lol
  • what is the meanest thing a girl can say to a guy so that he feels very bad
  • are you happy the way the majority of the nascar season cup has been why whynot
  • ouchhh please tell me is this a trapped nerve 039
  • what makes a man decide to get married
  • is there anyone who loves dogs more then anything who wants to be my friend
  • is unconditional love enough to maintain a relationship
  • nervous about nose piercing will it ever heal
  •  
  • why does weight matter so much
  • is she still interested
  • i need to make more friends help
  • any real work from home jobs
  • if you were a sales person what would you do in this scenario in the terms of the warranty
  • i want to sort ma best m8 out
  • hi am an mba in marketing 2005 doing job in an international bpo i am unable to my future in this bpo sector
  • track trivia martinsville speedway
  • should i leave him when he is going through a hard time
  • hi i m planning to go my country so i will be 25 weeks pregnant so my doctor said no is it safe to go
  • who are you rooting for in the chase now to win
  • prove to me you are smart
  • which is better arranged or love marriage
  •  Homepage | Add to favorites | Contact us | Exchange links | LOGIN | Site map | 
    Copyright© 2008 hznm.com        Site made:CFZ