p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional JavaScript for Web Developers 3rd edition (http://p2p.wrox.com/forumdisplay.php?f=669)
-   -   JSON/AJAX with ASP.Net (http://p2p.wrox.com/showthread.php?t=89617)

bnorg March 15th, 2013 01:13 PM

JSON/AJAX with ASP.Net
 
I read that ".getJSON is just shorthand for the ajax() function" because it only "wraps it" and doesn't do anything special on its own. I also read ".getJSON, like all ajax requests, is asynchronous by default, so your response will happen before the request completes. Therefore take a look at a synchronous request which will pause script execution until you get a response. However, the user cannot do anything until the response is received."

In this case 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. I know this because I trace the code execution in Visual Studio's debugging mode, and by setting breakpoints, can observe what is processed when and check the values of variables.

When the server request executes this JSON string is built successfully:
{"LocCity":"MyCity","LocState":"WA","LocZip":"9876 5",
"NameFirst":"Test","NameFullname":"Test User","NameInit":"",
"NameLast":"User","NameTitle":"","Street":"123 W Main St"}

Ajax() is used so async:false can be set and so execution will be synchronous according to the above suggestion to try to force the url to be run first before the success function . The response fails indicating no value in the return variable and then the request executes creating a valid JSON object too late for the return response. This shows that the process still ran asynchronously despite async:false. I can only conclude that a system bug is
preventing execution from performing according to instructions. The code functions properly but asynchronous processing puts the "cart before the horse."

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


All times are GMT -4. The time now is 05:41 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.