I am developing a ASP.NET WebForms website that uses JQuery to fetch some data from a ASP.NET ASHX handler. This data is an array of objects of a class that is user-defined. Following is the code of Handler:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using WRG_2._1.WRGCore;
namespace WRG_2._1.Handlers
{
/// <summary>
/// Summary description for ResponseFetcher
/// </summary>
public class ResponseFetcher : IHttpHandler, System.Web.SessionState.IReadOnlySessionState
{
public void ProcessRequest(HttpContext context)
{
List<Topic> comments = new List<Topic>() {
new Topic(){ Title=DateTime.Now.ToString() +":"+ DateTime.Now.Millisecond },
new Topic(){ Title=DateTime.Now.ToString() +":"+ DateTime.Now.Millisecond },
new Topic(){ Title=DateTime.Now.ToString() +":"+ DateTime.Now.Millisecond },
new Topic(){ Title=DateTime.Now.ToString() +":"+ DateTime.Now.Millisecond },
new Topic(){ Title=DateTime.Now.ToString() +":"+ DateTime.Now.Millisecond },
new Topic(){ Title=DateTime.Now.ToString() +":"+ DateTime.Now.Millisecond },
new Topic(){ Title=DateTime.Now.ToString() +":"+ DateTime.Now.Millisecond },
new Topic(){ Title=DateTime.Now.ToString() +":"+ DateTime.Now.Millisecond },
new Topic(){ Title=DateTime.Now.ToString() +":"+ DateTime.Now.Millisecond },
};
JavaScriptSerializer jss = new JavaScriptSerializer();
string sJSON = jss.Serialize(comments);
context.Response.Write(sJSON);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
I am fetching data from JQuery Ajax like this:
$(document).ready(function () {
var url = '/Handlers/ResponseFetcher.ashx';
$.ajax({
url: url,
type: "POST",
data:
JSON.stringify({ val1: 2, val2: 3 })
,
dataType: "json",
cache: true,
beforeSend: function () {
now = (new Date()).getTime();
if (localCache.exist(url)) {
tDiff = now - cacheTime;
if (tDiff < 20000) {
loadData(localCache.get(url));
return false;
}
}
return true;
},
complete: function (jqXHR, textStatus) {
localCache.set(url, jqXHR, loadData);
}
});
});
function loadData(data) {
console.log(data);
$(data.responseJSON).each(function (i) {
$('#responsecontainer').html = data.responseJSON[i].Title;
});
}
The function loadData() is getting the data perfectly. But its not adding it to #responsecontainer div. Please help!
Note that the class Topic can have null variables too.