簡體   English   中英

幫助我被困在嘗試解析從我的Web服務返回的Json字符串

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

我不確定是要獲取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));
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM