帮助我卡住试图解析Json字符串从我的web服务返回

Help I am stuck trying to parse Json string returned from my webservice

本文关键字:字符串 我的 web 返回 服务 Json 帮助      更新时间:2023-09-26

我不完全确定如果我得到JQuery或只是一个字符串,甚至在应用scriptservice属性和设置ResponseFormat属性Json之后。

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public ArrayList GetRoles()
{
      ArrayList arr = new ArrayList();
      arr.Add("manager");
      arr.Add("Project manager");
      arr.Add("Super Admin");
      arr.Add("Admin");
      arr.Add("Customer Rep");
      arr.Add("Sales Rep");
      arr.Add("Help Desk");
      arr.Add("Supervisor");
      arr.Add("Client");
    return arr;
}

我在前端得到的(当我使用弹出窗口查看它时)是所有值的字符串,用逗号连接和分隔。下面的代码似乎无法在下拉列表中显示列表。任何帮助都将不胜感激。

 $.each(msg.d, function (i, item) {
                          if (item) {
                                alert(i);
                                alert(item);
                                $("<%= SelectRole.ClientID %>").append($("<option></option>").attr("value", i).text(item));
                          }
                    });

因为它是一个用逗号分隔的连接字符串,你可以试试这个

$.each(msg.split(","), function (i, item) {
    $("#<%= SelectRole.ClientID %>")
    .append($("<option></option>")
    .attr("value", "-1")
    .text("select role"));
    if (item) {
      alert(item);
      $("#<%= SelectRole.ClientID %>")
      .append($("<option></option>")
      .attr("value",i)
      .text(item));
    }
});
相关文章: