Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create a cross domain ajax POST call but when I run the ajax function below, it displays output as 'undefined' instead of the data from the database.

JavaScript
<script type="text/javascript">

    $(document).ready(function () {
        $.support.cors = true;
        $.ajax({
            type: "POST",
            crossDomain: true,
            contentType: "application/json; charset=utf-8",
            url: "home.aspx/BindDatatable",
            data: "{}",
            dataType: "json",
            success: function (data) {
                for (var i = 0; i < data.d.length; i++) {
                    $("#tbDetails").append("<tr><td>" + data.d[i].Name + "</td><td>" + data.d[i].Ln+ "</td><td>" + data.d[i].Events + "</td></tr>");
                }
            },
            error: function (result) {
                alert("Error");
            }
        });
    });

</script>


Updated Server side method:
C#
[WebMethod]
         public static string BindDatatable()
        {
            DataTable dt = new DataTable();
            List<UserDetails> details = new List<UserDetails>();

            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["######"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("############", con))
                {
                    con.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);
                    foreach (DataRow dtrow in dt.Rows)
                    {
                        UserDetails user = new UserDetails();
                        user.Name = dtrow["###"].ToString();
                        user.Ln = dtrow["###"].ToString();
                        user.Events = dtrow["###"].ToString();
                        details.Add(user);
                    }
                }
            }
              JavaScriptSerializer serializer = new JavaScriptSerializer();
             return serializer.Serialize(details);
        }


any help, as to how I can make the above function work. Do I need further define the cross domain headers on my server side, if so, any further examples would be very beneficial.

Thank you
Posted
Updated 18-Mar-15 6:27am
v2

1 solution

That is because the result is an array, that is being return to your client.

C#
List<userdetails> details = new List<userdetails>(); // new generic list

// At the end
return details.ToArray(); // convert to array and send to client
</userdetails></userdetails>


But you're trying to access an array member of this response array; such as a member d of object that is object[]. Which is not valid.

Since there is no member d for data that is returned, that is why it is always showing undefined to you. Try this code, to get the elements from the list itself...

JavaScript
for (var i = 0; i < data.length; i++) {
    $("#tbDetails").append("<table><tbody><tr><td>" + 
                           data[i].Name + "</td><td>" + 
                           data[i].Ln+ "</td><td>" + 
                           data[i].Events + 
                           "</td></tr></tbody></table>");
}


This code would now do the task that you're trying to complete. For more on JavaScript arrays, please refer to following links,

http://javascript.info/tutorial/array[^]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections[^]
 
Share this answer
 
Comments
miss786 18-Mar-15 12:30pm    
Hi, thank you for your explanation. I tried your suggestion, i am sorry to inform, i was unable to get any output. I am now getting blank's in my table id from the ajax function. I tried changing my server code to output json string instead of array but I am still experiencing the no data showing in my table id. (please the my main post for further reference).

Any further advise would be very helpful. Thank you

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