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.

3
  • Did you return your response as json format from WebMethod? Commented Nov 22, 2013 at 5:38
  • I followed this stackoverflow.com/questions/5364343/… where adding [ScriptMethod(ResponseFormat = ResponseFormat.Json)] will automatically return result as json format Commented Nov 22, 2013 at 5: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. Commented Dec 7, 2013 at 1:03

1 Answer 1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.