Virendra Maurya

Virendra Maurya

  • NA
  • 9
  • 4.9k

JQuery ajax get returns null

Sep 12 2018 6:02 AM
// THIS IS MY JAVA SCRIPT CODE
  1. <script type="text/javascript">  
  2.         function getAllMessages() {  
  3.             $.ajax({  
  4.                 url: "TestService.asmx/GetAllMessages",  
  5.                 dataType: "json",  
  6.                 method: "GET",  
  7.                 contentType: "application/json",  
  8.                 success: onSuccess,  
  9.                 error: function (err) {  
  10.                     console.log(err);  
  11.                 }  
  12.             });  
  13.         }  
  14.   
  15.         function onSuccess(data) {  
  16.             console.log(data);  
  17.             var messageTable = $("#messages tbody");  
  18.             alert(data);  
  19.             $(data).each(function (index, msg) {  
  20.                 var apnString = "<tr><td>" + msg.ID + "</td><td>" + msg.Email + "</td><td>" + msg.Message + "</td><td>" + msg.TimeStamp + "</td></tr>";  
  21.                 messageTable.append(apnString);  
  22.             });  
  23.         }  
  24.     </script>  
// WebMethod Code
 
  1. SQLHelper clSQLHelper = new SQLHelper();  
  2.   
  3.         [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]  
  4.         [WebMethod]  
  5.         public void GetAllMessages()  
  6.         {  
  7.             List<Messages> listMsg = new List<Messages>();  
  8.             DataTable dtMsg = clSQLHelper.PGSQLExecuteReader("get_all_messages"new List<NpgsqlParameter>(), "Messages");  
  9.             if (dtMsg != null && dtMsg.Rows.Count > 0)  
  10.             {  
  11.   
  12.                 foreach (DataRow dr in dtMsg.Rows)  
  13.                 {  
  14.                     Messages Msg = new Messages();  
  15.                     Msg.ID = Convert.ToInt32(dr["auto_id"]);  
  16.                     Msg.Email = dr["email_id"].ToString();  
  17.                     Msg.Message = dr["message"].ToString();  
  18.                     Msg.TimeStamp = Convert.ToDateTime(dr["created_on"], new CultureInfo("en-IN"));  
  19.                     listMsg.Add(Msg);  
  20.                 }  
  21.   
  22.                 JavaScriptSerializer js = new JavaScriptSerializer();  
  23.                 Context.Response.Write(js.Serialize(listMsg));  
  24.             }  
  25.         }  
 Inside success callbak function, data value is null.

Answers (5)