Reputation: 620
I am trying to create cross domain ajax script but I am little struggling as to why the ajax function is outputting undefined
in the table id, instead of correct dynamic data.
$(document).ready(function () {
$.support.cors = true;
$.ajax({
type: "Post",
crossDomain: true,
contentType: "application/json; charset=utf-8",
url: "http://localhost:27335/test2.aspx/BindDatatable",
data: "{}",
dataType: "json",
success: function (data) {
console.log(data)
for (var i = 0; i < data.d.length; i++) {
$("#tbDetails").append("<tr><td>" + data.d[i].Name+ "</td><td>" + data.d[i].Loan + "</td><td>" + data.d[i].Evnt + "</td></tr>");
}
},
error: function (result) {
alert("Error");
}
});
});
Thanks in advance for any assistance.
Upvotes: 0
Views: 449
Reputation: 4635
Follow below steps
1.Debug your method BindDatatable()
2.In return serializer.Serialize(details);
You got any result or not ?
if not then serialized with different serializer e.g. Newtonsoft. you can found more details here Convert JSON String To C# Object
$(document).ready(function () {
$.support.cors = true;
$.ajax({
type: "Post",
crossDomain: true,
contentType: "application/json; charset=utf-8",
url: "http://localhost:27335/test2.aspx/BindDatatable",
data: "{}",
dataType: "json",
success: function (data) {
alert(data.toSource()) ;
console.log(data);
for (var i = 0; i < data.d.length; i++) {
$("#tbDetails").append("<tr><td>" + data.d[i].Name+ "</td><td>" + data.d[i].Loan + "</td><td>" + data.d[i].Evnt + "</td></tr>");
}
},
error: function (result) {
alert("Error");
}
});
});
This line alert(data.toSource())
var parsedData = $.parseJSON(data);
Upvotes: 2
Reputation: 86
You Webmethod BindDatatable() is returning you a JSON formatted data. You need to parse it before using.
You can do it like as below:
Your Ajax post would be like:
$.ajax({
type: "Post",
crossDomain: true,
contentType: "application/json; charset=utf-8",
url: "http://localhost:27335/test2.aspx/BindDatatable",
data: "{}",
dataType: "json",
success: function (data) {
console.log(data)
var parsedData=$.parseJSON(data);
for (var i = 0; i < parsedData.d.length; i++) {
$("#tbDetails").append("<tr><td>" + parsedData.d[i].Name+ "</td><td>" + parsedData.d[i].Loan + "</td><td>" + parsedData.d[i].Evnt + "</td></tr>");
}
},
error: function (result) {
alert("Error");
}
});
Here I have made use on parsedData. Instead of the data. You can have a look..
Hope this helps!!
Upvotes: 1