0

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.

10
  • in the table id what ID are we talking about? and what's the output of console.log(data) Commented Mar 19, 2015 at 10:03
  • Have you debugged it using Firebug to see what (if anything) is being returned from the ajax request? Commented Mar 19, 2015 at 10:04
  • dataType: "jsonp", isn't it be there instead? Commented Mar 19, 2015 at 10:04
  • What is the content of the data argument? Commented Mar 19, 2015 at 10:04
  • 1
    What is the console-output of console.log(data)? Commented Mar 19, 2015 at 10:30

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for thorough explanation. my server code is returning data, but the json data format in jsonlint is failing the output string. So, I am currently figuring this out. Thanks.
always welcome. Jsonlint is your validator of json and it is very helpful.
1

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!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.