First of all we need to create asmx service itself which returns some POCO object. Let’s create some abstract QueueService:
1: [WebService(Namespace = "http://example.com")]
2: [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
3: [ToolboxItem(false)]
4: [ScriptService]
5: public class QueueService : WebService
6: {
7: [WebMethod]
8: [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
9: public QueueItem GetQueueItem(string itemId)
10: {
11: var queueRepository = new QueueRepository();
12: var queueItem = queueRepository.GetById(new Guid(itemId));
13: return queueItem;
14: }
15: }
1: [Serializable]
2: public class QueueItem
3: {
4: public QueueItemStatus Status { get; set; }
5: public int Progress { get; set; }
6: }
The next step will be changing of the web.config, which should be located in the same folder where asmx file is, or location of asmx file should inherit it from parent folders. Here is the minimal required configuration:
1: <?xml version="1.0" encoding="utf-8" ?>
2: <configuration>
3: <system.web>
4: <webServices>
5: <protocols>
6: <add name="HttpPost"/>
7: </protocols>
8: </webServices>
9: <httpHandlers>
10: <remove verb="*" path="*.asmx"/>
11: <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
12: </httpHandlers>
13: </system.web>
14: </configuration>
This was the server part. Now we need to consume the web service from jquery ajax. Here is the example:
1: $.ajax({
2: url: "http://example.com/QueueService.asmx/GetQueueItem",
3: type: "POST",
4: contentType: "application/json; charset=utf-8",
5: data: "{'itemId':'...'}",
6: dataType: "json",
7: success: function(r) {
8: if (!r) {
9: // handle error
10: }
11: var result = r.d;
12: if (!result) {
13: // handle error
14: }
15: ...
16: },
17: error: function(xhr, status, error) {
18: // handle error
19: }
20: });
After this you should be able to consume asmx web service which returns JSON from jquery ajax. If you will check requests in fiddler you should see something like this:
request:
POST /QueueService.asmx/GetQueueItem HTTP/1.1response:
x-requested-with: XMLHttpRequest
Accept-Language: en-US,fi;q=0.5
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json; charset=utf-8
Content-Length: 52
Connection: Keep-Alive
Pragma: no-cache
{'itemId':'…'}
HTTP/1.1 200 OKAnd there are no any additional efforts needed for serializing/deserializing object to JSON neither on server nor on client. Hope this guide will help in you work.
Content-Length: 152
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
{"d":{"__type":"QueueItem","Status":0,"Progress":0}}