使用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
答案 0 :(得分:0)
像这样传递
return new JavaScriptSerializer().Serialize(new { errMsg = "test" });