Wrox Programmer Forums
BOOK: Professional JavaScript for Web Developers 3rd edition
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers, 3rd Edition by Nicholas C Zakas; ISBN: 978-1-1180-2669-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers 3rd edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 15th, 2013, 01:13 PM
Authorized User
Join Date: Aug 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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();
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;

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
End Sub
End Class

Similar Threads
Thread Thread Starter Forum Replies Last Post
ajax in asp.net bhupesh ASP.NET 3.5 Basics 1 May 25th, 2011 09:52 AM
how do i write JSON-encoded in asp.net vb? nir987 ASP.NET 3.5 Basics 31 October 29th, 2010 04:26 AM
asp.net ajax tookit, included in Asp.net 3.5 PeterPeiGuo Visual Web Developer 2008 1 January 6th, 2009 04:25 AM
Turning a regular ASP.NET App into an AJAX ASP.Net donrafeal7 Ajax 2 August 31st, 2007 12:33 AM
Asp.net ajax everest General .NET 0 March 8th, 2007 12:21 AM

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