0

编码:

[WebMethod]
public static string [] GetMorechatMsgs(int toclient, int fromclient, int top)
{

   string [] List =new string[2];
    int chatcount = new ChatPage().GetAllMsgCount(toclient, fromclient);
    if (top <= chatcount)
    {
        string toreturn=new ChatPage().GetChat(fromclient, toclient, "", top);
       List[0]= toreturn;
       List[1] = chatcount.ToString();

    }
    else {
        List = null;
    }
    return List;
}

html:

 $.ajax({
                type: "POST",
                url: "ChatPage.aspx/GetMorechatMsgs",
                data: "{'toclient':'" + ToClient + "','fromclient': '" + fromClient + "','top': '" + $("#MsgCount").val() + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {

                    if (data.d != "") {

                       // how to read the returned table 
                    }
                    else {

                    }
                },
                error: function (xhr) {
                    alert("responseText: " + xhr.responseText);
                }

            });

如何读取成功返回的字符串数组?

4

2 回答 2

1

序列化你的字符串列表,即

将您的方法更改为:

[WebMethod]
public static string  GetMorechatMsgs(int toclient, int fromclient, int top)
{
  /// your usual code

  return new JavaScriptSerializer().Serialze(list);
}

并像这样读取返回的数据:

success: function (data) {
     var jsonData =$.parseJSON(data.d);

     for(var i=0; i<jsonData.length; i++){
          console.log(jsonData[i]);
     }
}
于 2013-09-04T12:26:19.477 回答
0

WebMethod 将以 形式返回您的字符串数组["string", "string", "string", ...],因此,只需按照 Manish 演示的方式循环遍历数组:

success: function (data) {
   var jsonData =$.parseJSON(data.d);
   for(var i=0; i<jsonData.length; i++){
       var theString = jsonData[i];
       //Do something with the string
 }

}

于 2013-09-04T12:36:13.837 回答