Get JSON data from Asp.Net web service into ExtJs JSON store

In one of my previous project which was based on SOA, we have used Asp.Net web service to get data from Sql Server. The front end was built using ExtJs. As every one no that Asp.Net web service uses soap protocol which is XML based protocol.

For ExtJs front end we have to use JSON store for data as we were getting data from some other source in JSON format. So we need a response from Asp.Net web service in JSON format. Following the code example of how to get JSON data from Asp.Net web service.
First of all you need to enter Script Service Attribute to the web service.
[System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
//code of service.
}
A web service with ScriptService attribute by default returns data in JSON format. However it might be the case that in web service some methods return data in XML format so for that you need to add ScriptMethod attribute to specific method which should return data in JSON format.Following is an example of it.
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
public List GetCustomers()
{
dbDataContext obj= new dbDataContext();
return dc.getCustomers().ToList();
}
So above method when called form ExtJs front end will return data in JSON format. Following is the ExtJs code to call the service.
var storeCustomer = new Ext.data.JsonStore({
proxy: new Ext.data.HttpProxy({
url: ‘Service.asmx/GetCustomers’,
headers: {
‘content-type’: ‘application/json’
}
}),
root: ‘d’,
autoLoad : true,
id: ‘Customer_Id’,
scope : this,
fields: [‘Customer_Id’, ‘CurrentSeller_Id’, ‘Name’]
});
That’s it and you have JSON data from Asp.Net web service to your ExtJs front end.


This entry was posted in Asp.Net Web Service, ExtJs, JSON on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

10 thoughts on “Get JSON data from Asp.Net web service into ExtJs JSON store”

  1. Ebru Göktürk

    Hi Hiren, I like your blog so much ,I want to learn something about Json Webservice and ExtJS
    How I get Json From Webservice without any tag.(Iwant to get json only) and How I can access Json with ExtJs. Can u help me ???

    Thanks Hiren….

    Reply ↓
  2. Ebru Göktürk

    Hi Hiren,
    I used Ext.data.JsonStore to access Json data but i want to do this with ExtJs 4.0 ,but it is not be when i write like your code ,Your code is study with ExtJs 3 ,How can access Json data in ExtJS 4.0 ???

    Reply ↓

Leave a Reply

Your email address will not be published. Required fields are marked *