2013-09-04 3 views
0

код:чтение функция успеха 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); 
       } 

      }); 

Как я могу читать возвращаемый массив строк на успех?

+0

Вы возвращаете json из webmethod? – Jai

ответ

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]); 
    } 
} 
+0

то как я могу их прочитать из javascript? – Sora

0

WebMethod будет вернуть массив строк в форму ["string", "string", "string", ...], поэтому просто проведите по решетке так, как продемонстрировал Маниш:

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 
} 

Смежные вопросы