I am using this function to populate my table in visual studio 2010. but success function returns null. in my web service i am using
Context.Response.Write(js.Serialize(listMsg));
// THIS IS MY JAVA SCRIPT CODE
function getAllMessages() {
$.ajax({
url: "TestService.asmx/GetAllMessages",
dataType: "json",
method: "GET",
contentType: "application/json",
success: function (data) {
var messageTable = $("#messages tbody");
$(data).each(function (index, msg) {
var apnString = "<tr><td>" + msg.ID + "</td><td>" + msg.Email + "</td><td>" + msg.Message + "</td><td>" + msg.TimeStamp + "</td></tr>";
messageTable.append(apnString);
});
},
error: function (err) {
console.log(err);
}
});
here is my webMethod code. The webservice is returning json data.
[System.Web.Script.Services.ScriptService]
public class TestService : System.Web.Services.WebService
{
SQLHelper clSQLHelper = new SQLHelper();
[WebMethod]
public void GetAllMessages()
{
List<Messages> listMsg = new List<Messages>();
DataTable dtMsg = clSQLHelper.PGSQLExecuteReader("get_all_messages", new List<NpgsqlParameter>(), "Messages");
if (dtMsg != null && dtMsg.Rows.Count > 0)
{
foreach (DataRow dr in dtMsg.Rows)
{
Messages Msg = new Messages();
Msg.ID = Convert.ToInt32(dr["auto_id"]);
Msg.Email = dr["email_id"].ToString();
Msg.Message = dr["message"].ToString();
Msg.TimeStamp = Convert.ToDateTime(dr["created_on"], new CultureInfo("en-IN"));
listMsg.Add(Msg);
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(listMsg));
}
}
}
There are two issues with the code you mentioned-
1. In the web method you are using the below line so you are not returning anything to the client call in ajax.
Context.Response.Write(js.Serialize(listMsg));
So you need to change the definition of the method to return string and return the response some thing like below-
[WebMethod(Description = "Description")]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public string GetAllMessages()
{
List<Messages> listMsg = new List<Messages>();
DataTable dtMsg = clSQLHelper.PGSQLExecuteReader("get_all_messages", new List<NpgsqlParameter>(), "Messages");
if (dtMsg != null && dtMsg.Rows.Count > 0)
{
foreach (DataRow dr in dtMsg.Rows)
{
Messages Msg = new Messages();
Msg.ID = Convert.ToInt32(dr["auto_id"]);
Msg.Email = dr["email_id"].ToString();
Msg.Email = dr["message"].ToString();
Msg.TimeStamp = Convert.ToDateTime(dr["created_on"], new CultureInfo("en-IN"));
listMsg.Add(Msg);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(listMsg);
}
2. You are not telling the ASMX web method how this method can be accessed
As you are calling the GET method on asmx you need to add below line on top of the web method
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
Importance of the above line -
This attribute is used to specify information for methods that can be called from client script. The attribute lets you specify which HTTP verb (GET or POST) can be used to invoke a method. It also lets you specify whether the response is formatted by using either JavaScript Object Notation (JSON) or XML.
Below are the details for other attrinutes -
UseHttpGet
Specifies whether the method will be invoked by using the HTTP GET command. The default is false.
ResponseFormat
Specifies whether the response will be serialized as JSON or as XML. The default is Json. The ResponseFormat property is useful to specify XML as the return type when the method returns an XmlDocument or an XmlElement object.
Please refer for more info on this https://docs.microsoft.com/en-us/dotnet/api/system.web.script.services.scriptmethodattribute?view=netframework-4.7.2