20

I'm a bit of a newb with both extJS and json. What is the most painless route to POSTing json data using extJS? I'm not really interested any GUI features, just using the framework to send some sample data.

Toni Frankola
  • 1,652
  • 15
  • 26
maximus
  • 2,417
  • 5
  • 40
  • 56

5 Answers5

25
Ext.Ajax.request({
   url: 'foo.php',    // where you wanna post
   success: passFn,   // function called on success
   failure: failFn,
   params: { foo: 'bar' }  // your json data
});
Krishna K
  • 1,925
  • 1
  • 14
  • 11
  • 23
    This will post URLencoded like data... IOW, the POST buffer will be foo=bar. If you replace the `params` for `jsonData` it will post raw JSON, so the POST buffer will be `{"foo":"bar"}` – SBUJOLD May 27 '10 at 12:48
  • In ExtJS 4.1 you can use the jsonData Member. – Chris Jul 03 '12 at 13:11
20

The following will identify as 'POST' request

 Ext.Ajax.request({
       url: 'foo.php',    // where you wanna post
       success: passFn,   // function called on success
       failure: failFn,
       jsonData: { foo: 'bar' }  // your json data
    });

The following will identify as 'GET' request

Ext.Ajax.request({
   url: 'foo.php',    // where you wanna make the get request
   success: passFn,   // function called on success
   failure: failFn,
   params: { foo: 'bar' }  // your json data
});
Sandeepan Kundu
  • 201
  • 2
  • 2
  • 2
    you can also use `method: 'POST' / 'GET'` parameter: http://docs.sencha.com/extjs/4.1.3/#!/api/Ext.Ajax-property-method – efirat Jun 25 '13 at 16:22
6

Just to add my two cents:

//
//Encoding to JSON:
//
var myObj = {
  visit: "http://thecodeabode.blogspot.com/"
};
var jsonStr = Ext.encode(myObj);


//
// Decoding from JSON
//
var myObjCopy = Ext.decode(jsonStr);
document.location.href = myObj.visit;
Ben
  • 1,203
  • 13
  • 8
3

The examples posted here show the basic idea. For complete details on all configurable options see the Ext.Ajax docs.

Brian Moeskau
  • 20,103
  • 8
  • 71
  • 73
0

Code Snippet:

 Ext.Ajax.request({
    url: "https://reqres.in/api/users",
    success: function (response) {
        Ext.Msg.alert("success", response.responseText);
    },
    failure: function () {
        Ext.Msg.alert("failure", "failed to load")
    },
    params: {
        "name": "morpheus",
        "job": "leader"
    }
});

Fiddle: https://fiddle.sencha.com/#view/editor&fiddle/28h1

Saurabh Nemade
  • 1,522
  • 2
  • 11
  • 20