简体   繁体   中英

Json Serialisers vs .net serialised classes for JQuery AJAX result data

What is the most effective way to pass data from .net (c#, vb.net) Web Service to the client side using JQuery AJAX ?

A) Using Newtonsoft JSON serialization eg

 <WebInvoke(Method:="*", ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/abc", BodyStyle:=WebMessageBodyStyle.Bare)>
    Public Function GetDontMissItems(JsonParams As RequestDataTypes.DontMissParams) As String
  objDontMissItems = Helper.Instance.GetDontMissNews(JsonParams.FeaturedCategoryId, QtyOfNumberOfItems, JsonParams.Randomize, If(JsonParams.NotInIDs = Nothing, "", JsonParams.NotInIDs))

   Dim strSerialzed As String = JsonConvert.SerializeObject(objDontMissItems)

   Return strSerialzed
   End Function

Or

B) passing a Serialized class from the web service to the AJAX call eg

<Serializable>
Public Class clsPoll
    Public Property answerID As Integer
    Public Property questionRef As String
    Public Property votePercentage As String

End Class



<WebMethod(EnableSession:=True)> _
    Public Function InsertPoll(ByVal jsonData As clsPoll) As List(Of clsPoll)
    Dim dtVotests As DataSet = objAnswer.CalculateVote(jsonData.questionRef, 1)
    Dim lstPoll As New List(Of clsPoll)
        For Each drVotests As DataRow In dtVotests.Tables(0).Rows
            Dim objPollTemp As New clsPoll
            objPollTemp.answerID = drVotests("id")
            objPollTemp.questionRef = jsonData.questionRef
            objPollTemp.votePercentage = drVotests("p")
            lstPoll.Add(objPollTemp)
        Next
    Return lstPoll
End Function

在服务器端像这样传递它

return new JavaScriptSerializer().Serialize(new { errMsg = "test" });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM