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

AJAX callback out-of-sync

$
0
0

Executing an Ajax callback from an .aspx page causes the success function to run before
the server request 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 this JSON string is built successfully:
     {"LocCity":"MyCity","LocState":"WA","LocZip":"98765",
     "NameFirst":"Test","NameFullname":"Test User","NameInit":"",
     "NameLast":"User","NameTitle":"","Street":"123 W Main St"}

This data is built for the querystring Get:
Dim custId As String = "custId=" & Convert.ToString(custNum)

This registers the JavaScript event handler:
    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

<script type="text/javascript">
    function getData() {
        var custId = $("#choice").text();
        $.ajax({
            url: "BookGetCust.aspx",
            data: custId,
            datatype: 'json',
            type: 'get',
            async: false,
            success: gotData()  *Inserting a variable argument as a prototype placeholder
 to match the success function parameter doesn't accomplish anything.
        })
    }
    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>

Ajax supposedly knows something is being returned so should make it available to the success function.
This builds the JSON string on the server and Response.Write is supposed to return the string:
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
        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>