调用.ajax方法得到未定义的json结果,返回json格式列表<字符串>

Getting undefined json result on calling .ajax method which return json format list<string>

本文关键字:json 列表 格式 lt gt 字符串 返回 结果 方法 ajax 未定义      更新时间:2023-09-26

我得到的是未定义的结果,而不是列表数组。这是我的代码

      $.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);
                }             });

这是我称之为的网络方法

[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;
    }

当我看到msg.d或msg值显示为未定义时,我的代码出了什么问题。

尝试使用

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

并将您的错误修改为这样,以获取更多指定错误

error: function(XMLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}