读取字符串表jquery ajax成功函数 [英] reading a string table jquery ajax success function

查看:94
本文介绍了读取字符串表jquery ajax成功函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

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

            });

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

How can i read the returned string array on success ?

推荐答案

序列化字符串列表,即

将您的方法更改为此:

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

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

并读取返回的数据,如下所示:

and read returned data like this:

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

     for(var i=0; i<jsonData.length; i++){
          console.log(jsonData[i]);
     }
}

这篇关于读取字符串表jquery ajax成功函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆