Json Serialisers vs .net用于JQuery AJAX结果数据的序列化类

时间:2014-08-06 13:50:49

标签: c# javascript jquery ajax json

使用JQuery AJAX将数据从.net(c#,vb.net)Web服务传递到客户端的最有效方法是什么?

A)使用Newtonsoft JSON序列化,例如

 <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

B)将Serialized类从Web服务传递给AJAX调用,例如

<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

1 个答案:

答案 0 :(得分:0)

服务器端的

像这样传递

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