Abhijeet Sonawane
Abhijeet Sonawane

Reputation: 680

Getting data from web service using ajax

I'm trying to get data from a webservice using ajax and POST method.

Ext.Ajax.request({
url: 'http://localhost......',
method:'POST',
   success: function(response) { console.log(response.responseText); },
  failure: function() { Ext.Msg.alert('Fail'); },
  jsonData:{ 
      /*Here I specify my request json*/
    }
}
  });

The above thing works fine but when i try to mimic the same in EXTJS store it responds with a error he server responded with a status of 415 (Unsupported Media Type)

EXTJS STORE CODE

Ext.define('IWM.store.JsonTest', {

    extend: 'Ext.data.Store',
    autoLoad: true,

    fields:['Name'],

    proxy: {

        type: 'ajax',
        method : 'POST',
        actionMethods: {
            create : 'POST',
            read   : 'POST',
            update : 'POST',
            destroy: 'POST'
        },
        jsonData:{
            /*JSON */
            }
        },
        url: 'http://localhost......',

        success: function(response) { console.log(response.responseText); },

        failure:function(){console.log("failed");},

        reader: {

            type: 'json',

            root: 'result',

            successProperty: 'success'

        }

    }

});

Upvotes: 0

Views: 1306

Answers (2)

Jaimie Whiteside
Jaimie Whiteside

Reputation: 1220

Following on from Oğuz Çelikdemir's answer, you may need to define a json writer for your proxy.

proxy: {
    type: 'ajax',
    url: 'http://your-url-address',
    reader: {
        type: 'json',
        root: 'json-root-value',
        idProperty: 'unique-property-in-json-file'
    },
    writer: {
        type: 'json'
    }
}

That should set the json mediatype header (assuming that's what your request is expecting).

Upvotes: 0

Oğuz Çelikdemir
Oğuz Çelikdemir

Reputation: 4980

First things first, try to change store config as below.

var IWMStore = new Ext.data.JsonStore({
   // it seems to me you had forgotten model
    model: 'YourDataModel',
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url: 'http://your-url-address',
        reader: {
            type: 'json',
            root: 'json-root-value',
            idProperty: 'unique-property-in-json-file'
        }
    }
});

Example JSON value :

// here, ctgMains is the root value, CUST_ASSORT_SECTION_ID is the idProperty
{"ctgMains":[{"CUST_ASSORT_SECTION_ID":"1","CTG_SECTION":"HORECA"},{"CUST_ASSORT_SECTION_ID":"7","CTG_SECTION":"SCO"},{"CUST_ASSORT_SECTION_ID":"3","CTG_SECTION":"TRADER"}]}

Second Attempt : Try to add headers property in your proxy definition, like below:

        proxy: {
            type: 'ajax',
            url: 'http://your-url-address',
            reader: {
                type: 'json',
                root: 'json-root-value',
                idProperty: 'unique-property-in-json-file'
            },
            headers: {
                'Content-type': 'application/json',
                'Accept': 'application/json'
            }
        }

Upvotes: 1

Related Questions