0

I am getting undefined result instead of list array. Here is my code

      $.ajax({
                type: "POST",
                url: "DeviceInfo.aspx/GetDeviceValues",
                data: {},
                async:true,
                contentType: "applciation/json; charset=utf-8",
                datatype: "json",
                success: function (msg) { var arr = msg.d; alert(arr); for (var i in arr) { alert(i) } },
                error: function (x, e) {
                    alert("The call to the server side failed. " + x.responseText);
                }             });

Here is the webmethod i am calling

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public static List<string> GetDeviceValues()
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        List<string> Row = new List<string>();
        Row.Add("First Device ");
        Row.Add("IMEI No Is 22323233");
        Row.Add("UI No is 23232");
        Row.Add("Msg No is 12");
        Row.Add("Active is status is true");
        Row.Add("InActive status is false");
        return Row;

    }

When i see the msg.d or msg value it showing as undefined, what's wrong with my code.

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
Sekhar Babu
  • 368
  • 2
  • 8
  • 27
  • Did you return your response as json format from WebMethod? – Krish R Nov 22 '13 at 05:38
  • I followed this http://stackoverflow.com/questions/5364343/asp-net-web-forms-json-return-result where adding [ScriptMethod(ResponseFormat = ResponseFormat.Json)] will automatically return result as json format – Sekhar Babu Nov 22 '13 at 05:41
  • Use something like FireBug to monitor the network requests, so you can see exactly what is being returned from the web service - if you're getting undefined back, your web service may be generating an error. I list to wrap lists like this up in an object, along with a boolean success/failure flag and an error message string. Wrap the guts of the web service in a try/catch loop, and in the catch block set the success flag to false, and set the returned objects error message to the exceptions error message. – John - Not A Number Dec 07 '13 at 01:03

1 Answers1

0

try to use

$.each(result, function (index, item) {
    ////try something in alert(item.d);
});

and modify your error to look like this to fetch more specify error

error: function(XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}
Neel
  • 11,625
  • 3
  • 43
  • 61