PDA

View Full Version : Simple Question AJAX vs. Ext.Direct for JSON



cgountanis
6 Oct 2009, 9:35 AM
I want to load this JSON file into my application for usage in setting JavaScript variables. Do you recommend I use Ext.Direct and have my server-side code do this eliminating a AJAX proxy or some other way? I am using Ext.Direct now in example form and it is working for form submit and button presses.

Anyone have an example of how I send request to URL below and parse out the initial extents plain and simple?

I am using .NET C# and Ext.Direct.

JSON FIle: http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/TaxParcel/AssessorsBasemap/MapServer?f=json&pretty=true

cgountanis
7 Oct 2009, 2:26 PM
I figured it out. It's not perfect but really proves you can run inline JS and fill variables using .NET classes on the fly without complicated AJAX calls.

Ext.OnReady{}


var uri = 'http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/TaxParcel/AssessorsBasemap/MapServer?f=json';
Sample.GetExtents(uri, function(e, o)
{
Ext.Msg.alert('Title', o.result);
});



Sample.cs Function GetExtents{}


[DirectMethod]
public string GetExtents(string content)
{
return StringGetWebPage(content);
}


private string StringGetWebPage(String uri)
{
const int bufSizeMax = 65536; // max read buffer size conserves memory
const int bufSizeMin = 8192; // min size prevents numerous small reads
StringBuilder sb;

// A WebException is thrown if HTTP request fails
try
{
// Create an HttpWebRequest using WebRequest.Create (see .NET docs)!
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

// Execute the request and obtain the response stream
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();

// Content-Length header is not trustable, but makes a good hint.
// Responses longer than int size will throw an exception here!
int length = (int)response.ContentLength;

// Use Content-Length if between bufSizeMax and bufSizeMin
int bufSize = bufSizeMin;
if (length > bufSize)
bufSize = length > bufSizeMax ? bufSizeMax : length;

// Allocate buffer and StringBuilder for reading response
byte[] buf = new byte[bufSize];
sb = new StringBuilder(bufSize);

// Read response stream until end
while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)
sb.Append(Encoding.UTF8.GetString(buf, 0, length));
}
catch (Exception ex)
{
sb = new StringBuilder(ex.Message);
}
return sb.ToString();
}