AJAX - 检索多个响应值?

时间:2016-12-21 02:11:19

标签: javascript jquery asp.net ajax

我有AJAX代码,它返回一个HTML字符串,以附加到当前div中的内容。

我现在还希望它返回一个值来更新隐藏字段HiddenFieldSortLetter,以便可以用于将来的AJAX调用。

我可以补充一下:

ByRef varAJAXHiddenFieldSortLetterNew As String 

的参数末尾
Public Shared Function fGetData

但是我怎么能改变

success: function (msg) { $("#<%=dvGridItems01.ClientID%>").append(msg.d); },

这样它还会更新隐藏字段HiddenFieldSortLetter?

        $.ajax(
        { type: "POST",
        url: "default.aspx/fGetData", 
        data: varData,
        contentType: "application/json; charset=utf-8", 
        dataType: "json",
        async: "true",
        cache: "false",
        success: function (msg) { $("#<%=dvGridItems01.ClientID%>").append(msg.d); }, 
        Error: function (x, e) { alert("Network error"); } }); 

ASP.NET 3.5 VB.NET:

<WebMethod()> _
    Public Shared Function fGetData(ByVal pageIndex As Integer, _
                                    ByVal isSearch As Integer, _
                                    ByVal searchText As String, _
                                    ByVal varAJAXHiddenFieldConsumerID As Object, _
                                    ByVal varAJAXHiddenFieldSortLetter As Object _
                                    ) As String

        Dim strReturn As String = ""

        Dim c1 As New cVDisplayGridItems
        With c1
            .pConsumerID = varAJAXHiddenFieldConsumerID
            .pAJAXSortLetter = varAJAXHiddenFieldSortLetter

            If isSearch = 1 Then
                'Search results
                .pSearchTerm = searchText.Trim
                .fGenerateHTMLForSearch()
            Else
            End If

            strReturn = .pHTML

        End With

        Return strReturn

    End Function

1 个答案:

答案 0 :(得分:1)

因此,对于服务器端,我创建了一个结构来保存我想要一次返回的两个值。我序列化并返回它。

   Public Structure dataResponse
    Dim  pHTML as String
    Dim  hidden as String
end Structure
<WebMethod()> _
Public Shared Function fGetData(ByVal pageIndex As Integer, _
                                ByVal isSearch As Integer, _
                                ByVal searchText As String, _
                                ByVal varAJAXHiddenFieldConsumerID As Object, _
                                ByVal varAJAXHiddenFieldSortLetter As Object _
                                ) As String

    Dim strReturn As String = ""

    Dim c1 As New cVDisplayGridItems
    With c1
        .pConsumerID = varAJAXHiddenFieldConsumerID
        .pAJAXSortLetter = varAJAXHiddenFieldSortLetter

        If isSearch = 1 Then
            'Search results
            .pSearchTerm = searchText.Trim
            .fGenerateHTMLForSearch()
        Else
        End If

        strReturn = .pHTML

    End With
    Dim retStruct as new dataResponse()
    retStruct.pHTML = strReturn
    retStruct.hidden = somefunctionthatgetsthehidden()
      System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();


    Return ser.Serialize(retStruct)
End Function

然后在客户端上我改变了成功:看起来像这样......(如果你将控件上的ClientIdMode设置为Static,你不必使用&lt;%= stuff。

success: function (msg) {
    var myRes = JSON.parse(msg.d);
    $("#dvGridItems01").append(myRes.pHTML);
    $("#hdnTextBox").val(myRes.hidden);

 },