ExtJs 4.1 : How to send json data in the request body using Ext.Ajax.request()?

leaf picture leaf · Sep 17, 2012 · Viewed 32.2k times · Source

I would like to send json data using Ext.Ajax.request() then access it in ASP.NET using Request.InputStream which is the content of the request body. I need a way to tell ExtJs to write the data in the request body as it is done while using an Ext.data.proxy.Ajax.

Answer

egerardus picture egerardus · Sep 17, 2012

Specify POST method and just use the request's jsonData config:

Ext.Ajax.request({
    url: 'myUrl',
    method: 'POST',
    params: {
        requestParam: 'notInRequestBody'
    },
    jsonData: 'thisIsInRequestBody',
    success: function() {
        console.log('success');
    },
    failure: function() {
        console.log('woops');
    }
});

If you want a record written as JSON you can use a JSON writer like this also.

var writer = Ext.create('Ext.data.writer.Json'),
    record = Ext.getStore('SomeStoreID').first();

Ext.Ajax.request({
    url: 'myUrl',
    method: 'POST',
    params: {
        requestParam: 'notInRequestBody'
    },
    jsonData: writer.getRecordData(record),
    success: function() {
        console.log('success');
    },
    failure: function() {
        console.log('woops');
    }
});