Click here to Skip to main content
15,125,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi i am trying to create dynamic html elements on ajax success response but getting a facing different kind of problem.

In browser console i can see the success result:
[{"ResponseTime":"13/6/2019 24:51","TicketId":0,"UserId":"ricky.global9@gmail.com","Subject":"Domain problem","Department":"Sales","Description":"Content 2","FileId":"NULL","ResponseType":null},{"ResponseTime":"13/6/2019 23:51","TicketId":0,"UserId":"ricky.global9@gmail.com","Subject":"_Payment_issue","Department":"Support","Description":"Content 1","FileId":"NULL","ResponseType":null}]{"d":null}


Here
{"d":null}
is suspicious...
But ajax also triggers error function "undefined"

Here is my ajax
JavaScript
<pre>
var tid = '2723021';

$.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "../../Service.asmx/GetTicketDetails",
                data: "{ TicketID: '" + tid + "'}",
                dataType: "json",
                success: function (data) {
                    var SignalTable = $('#ticketDetails');
                    SignalTable.empty();

                    $(data).each(function (index, sigs) {
                        SignalTable.append('<p>'
                            + sigs.ResponseTime
                            + '</p><p>' + sigs.UserId
                            + '</p><p> ' + sigs.Subject
                            + '</p ><p>' + sigs.Department
                            + '</p><p>' + sigs.Description
                            + '</p><p>' + sigs.FileId + '</p><p>');
                    });
                },
                error: function (err) {
                    alert('Err ' + err);
                }
            });



The webmethod
C#
<pre>
public void GetTicketDetails(string TicketID)
        {
            List<SupportTicket> SideTickets = new List<SupportTicket>();

            string CS = ConfigurationManager.ConnectionStrings["iqCon"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("procSidebarTicketDetails", con);
//Precedure query SELECT Response_time, User_email, Support_subject, Support_department, Support_description, File_id FROM Support_tickets WHERE Ticket_id = @Ticketid;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Ticketid", TicketID);

                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    SupportTicket tickets = new SupportTicket();
                    tickets.ResponseTime = rdr["Response_time"].ToString();
                    tickets.UserId = rdr["User_email"].ToString();
                    tickets.Subject = rdr["Support_subject"].ToString();
                    tickets.Department = rdr["Support_department"].ToString();
                    tickets.Description = rdr["Support_description"].ToString();
                    tickets.FileId = rdr["File_id"].ToString();
                    SideTickets.Add(tickets);
                }
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(SideTickets));
        }



And the class object
C#
public class SupportTicket
        {
            public string ResponseTime { get; set; }
            public int TicketId { get; set; }
            public string UserId { get; set; }
            public string Subject { get; set; }
            public string Department { get; set; }
            public string Description { get; set; }
            public string FileId { get; set; }
            public string ResponseType { get; set; }            
        }


What I have tried:

I tried all possibilities from my end but couldn't find where is am doing wrong.
Posted
Comments
Patrice T 15-Jun-19 2:30am
   
And it tells you its name and position of error ?
lmoelleb 15-Jun-19 8:51am
   
And what does debugging show you? Incoming data on the backend as expected? Outgoing data? Where does the JavaScript encounter "nothing"? What did you expect this "nothing" to be? In case you think debugging is an advanced topic you will learn later, you are wrong. Debugging is simple and extremely useful. I can't think of a more important skill, no matter if you are just starting programming or have 30+ years experience. Learning to use a debugger takes 5-10 minutes. With both JavaScript and C# backend code, you need to learn VS and browser debugging. So then you might be looking at spending a whopping 10-15 minutes on a skill that will save you hours on even the smallest project.
F-ES Sitecore 17-Jun-19 3:50am
   
Don't use Context.Response.Write inside a web method. That is where your "[{"ResponseTime"...." data is coming from and the "d:null" is being generated by the webmethod. Google how to use webmethods with ajax, there are loads of examples out there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900