1. #1
    Ext User
    Join Date
    Apr 2008
    Posts
    20
    Vote Rating
    0
    alim.karim is on a distinguished road

      0  

    Default 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:

    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);
    	   	}
    	});
    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?

  2. #2
    Sencha - Community Support Team jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    Frederick MD, NYC, DC
    Posts
    16,337
    Vote Rating
    75
    jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all

      0  

    Default


    i would do it like this, so you have full control:
    Code:
    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
    Code:
    <?
    
    $myObj = json_decode(strip_slashes($_REQUEST['ajax_req']));
    
    print $myObj->param1;
    
    print $myObj->obj1->a;
    
    ?>

  3. #3
    Sencha - Community Support Team jay@moduscreate.com's Avatar
    Join Date
    Mar 2007
    Location
    Frederick MD, NYC, DC
    Posts
    16,337
    Vote Rating
    75
    jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all jay@moduscreate.com is a name known to all

      0  

    Default


    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.

  4. #4
    Ext User
    Join Date
    Apr 2008
    Posts
    20
    Vote Rating
    0
    alim.karim is on a distinguished road

      0  

    Default


    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.

  5. #5
    Ext User angelflaree's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Vote Rating
    0
    angelflaree is on a distinguished road

      0  

    Arrow


    yes, I have the same question.
    and I use:
    Code:
    jsondata: myJsondata
    instead of
    Code:
    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()
    good good study,
    day day up ; )

  6. #6
    Ext User
    Join Date
    Jul 2007
    Posts
    3,128
    Vote Rating
    1
    devnull is an unknown quantity at this point

      0  

    Default


    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?

  7. #7
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,483
    Vote Rating
    35
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    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.

  8. #8
    Ext User
    Join Date
    Jul 2007
    Posts
    3,128
    Vote Rating
    1
    devnull is an unknown quantity at this point

      0  

    Default


    I think theres sill some confusion..
    I am curious about what is the difference here?

    jsonData: someObject
    vs
    params: someObject

  9. #9
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,483
    Vote Rating
    35
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    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:

    Code:
    param1=foo&param2=bar&myJson={foo:'bar'}
    No HTTP Parameters in the first version.

    The second version is standard HTTP parameters.

  10. #10
    Ext User
    Join Date
    Jul 2007
    Posts
    3,128
    Vote Rating
    1
    devnull is an unknown quantity at this point

      0  

    Default


    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

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar