user3070072
user3070072

Reputation: 620

ajax function not returning data in C#

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

Answers (2)

MSTdev
MSTdev

Reputation: 4635

Follow below steps

1.Debug your method BindDatatable()

2.In return serializer.Serialize(details); You got any result or not ?

  1. If yes then write below line in your code.

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())

  1. IF you got any json then verify your json in jsonlint.com. if it is verified then make sure it is valid as per requirements and also parse.

var parsedData = $.parseJSON(data);

  1. It will work.

Upvotes: 2

Praveen
Praveen

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

Related Questions