Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Ajax callback execution reversed

$
0
0

Trying to execute Ajax callback from an .aspx page with Visual Studio 2010.  But
debugging shows that the Ajax routine runs the success function before the server url,
so it fails because of lack of data.  The server access runs successfully afterwards when
it is too late.  Used the approach below with async=false to try to force the url to be run
first before the success function but that didn't work.  When the server request executes
the JSON string is built successfully and I use Response.Write(json) to return it.  It looks
like ASP.Net (with the Ajax library) is executing the functions out of order.

Dim custId As String = "custId=" & Convert.ToString(custNum)
<script type="text/javascript">
    function getData() {
        var custId = $("#choice").text();
        $.ajax({
            url: "BookGetCust.aspx",
            data: custId,
            datatype: 'json',
            type: 'get',
            async: false,
            success: gotData()
** It isn't allowed to specify a predetermined argument in the
success function declaration.       })
    }

** I assume it is Ajax's job to know that a result is returned so that when you use a name for the
JSON string result it gets treated accordingly.
    function gotData(cust) {
        if (cust != null) {
            $('#name').text() = cust.NameFullname;
            $('#street').text() = cust.Street;
            $('#city').text() = cust.LocCity;
            $('#state').text() = cust.LocState;
            $('zipcode').text() = cust.LocZip;
         }
    }
</script>
    Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        If Not ClientScript.IsClientScriptBlockRegistered("get") Then
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "get", "getData()")
        End If
    End Sub

This is the JSON string that the code below produced (real data changed):
{"LocCity":"MyCity","LocState":"WA","LocZip":"98765",
"NameFirst":"Test","NameFullname":"Test User","NameInit":"",
"NameLast":"User","NameTitle":"","Street":"123 W Main St"}

Imports System.Runtime.Serialization.Json
Partial Class BookGetCust Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim custNum As Integer = Convert.ToInt32(Request("custId"))
        Dim custDLL As New ShopValidDLL
        Dim street As String = custDLL.ValStreet(custNum)
        Dim cust As New ShipCust
        cust = custDLL.ShipCust(custNum, street)
        Dim stream As New memorystream()
        Dim serializer As New DataContractJsonSerializer(GetType(ShipCust))
        serializer.WriteObject(stream, cust)
        stream.Position = 0
        Dim streamRdr As New StreamReader(stream)
        Dim jsonString As String = streamRdr.ReadToEnd


** Hopefully it is being returned correctly by the Response method.

        Response.Write(jsonString)
        Response.End()
    End Sub
End Class


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>