Skip to content

Commit c943705

Browse files
committed
Simplified code examples and reworked recipe text.
1 parent 457b3a4 commit c943705

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

chapters/ajax/ajax_request_without_jquery.md

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: recipe
3-
title: AJAX Request Without jQuery
4-
chapter: AJAX
3+
title: Ajax Request Without jQuery
4+
chapter: Ajax
55
---
66
## Problem
77

@@ -11,37 +11,6 @@ You want to load data from your server via AJAX without using the jQuery library
1111

1212
You will use the native <a href="http://en.wikipedia.org/wiki/XMLHttpRequest" target="_blank">XMLHttpRequest</a> object.
1313

14-
Begin by making sure the XMLHttpRequest object exsits.
15-
16-
{% highlight coffeescript %}
17-
# XMLHttpRequest.coffee
18-
19-
if (typeof @XMLHttpRequest == "undefined")
20-
console.log 'XMLHttpRequest is undefined'
21-
22-
@XMLHttpRequest = ->
23-
try
24-
return new ActiveXObject("Msxml2.XMLHTTP.6.0")
25-
catch error
26-
try
27-
return new ActiveXObject("Msxml2.XMLHTTP.3.0")
28-
catch error
29-
try
30-
return new ActiveXObject("Microsoft.XMLHTTP")
31-
catch error
32-
throw new Error("This browser does not support XMLHttpRequest.")
33-
{% endhighlight %}
34-
35-
We can also set up some status codes.
36-
37-
{% highlight coffeescript %}
38-
READYSTATE_UNINITIALIZED = 0
39-
READYSTATE_LOADING = 1
40-
READYSTATE_LOADED = 2
41-
READYSTATE_INTERACTIVE = 3
42-
READYSTATE_COMPLETE = 4
43-
{% endhighlight %}
44-
4514
Let's set up a simple test HTML page with a button.
4615

4716
{% highlight html %}
@@ -60,15 +29,25 @@ Let's set up a simple test HTML page with a button.
6029
</html>
6130
{% endhighlight %}
6231

63-
Let's finish our XMLHttpRequest.coffee by adding a 'click' event listener then create our XMLHttpRequest object.
32+
When the button is clicked, we want to send an Ajax request to the server to retrieve some data. For this sample, we have a small JSON file.
33+
34+
{% highlight javascript %}
35+
// data.json
36+
{
37+
message: "Hello World"
38+
}
39+
{% endhighlight %}
40+
41+
Next, create the CoffeeScript file to hold the page logic. The code in this file creates a function to be called when the Load Data button is clicked.
6442

6543
{% highlight coffeescript linenos %}
44+
# XMLHttpRequest.coffee
6645
loadDataFromServer = ->
6746
req = new XMLHttpRequest()
6847

6948
req.addEventListener 'readystatechange', ->
70-
if req.readyState is READYSTATE_COMPLETE
71-
if req.status is 200 or req.status is 304
49+
if req.readyState is 4 # ReadyState Compelte
50+
if req.status is 200 or req.status is 304 # Success result codes
7251
data = eval '(' + req.responseText + ')'
7352
console.log 'data message: ', data.message
7453
else
@@ -83,14 +62,41 @@ loadDataButton.addEventListener 'click', loadDataFromServer, false
8362

8463
## Discussion
8564

86-
In the above code, we create a new XMLHttpRequest instance on line 2. Then, we add an event listener for the readystatechange event. This fires whenever the XMLHttpRequest readyState changes.
65+
In the above code we essentially grab a handle to the button in our HTML (line 16) and add a *click* event listener (line 17). In our event listener, we define our callback function as loadDataFromServer.
66+
67+
We define our loadDataFromServer callback beginning on line 2.
8768

88-
In the event handler we check to see if the readyState = READYSTATE_COMPLETE (value of 4). Then, we check to see if the status is either 200 or 304, both values are success indicators.
69+
We create a XMLHttpRequest request object (line 3) and add a *readystatechange* event handler. This fires whenever the request's readyState changes.
70+
71+
In the event handler we check to see if the readyState = 4, indicating the request has completed. Then, we check the request status value. Both 200 or 304 represent a succsessful request. Anything else represents an error condition.
8972

9073
If the request was indeed successful, we eval the JSON returned from the server and assign it to a data variable. At this point, we can use the returned data in any way we need to.
9174

9275
The last thing we need to do is actually make our request.
9376

94-
Line 12 opens a 'GET' request to retreive the data.json file.
77+
Line 13 opens a 'GET' request to retreive the data.json file.
78+
79+
Line 14 sends our request to the server.
80+
81+
## Older Browser Support
82+
83+
If your application needs to target older versions of Internet Explorer, you will need to ensure the XMLHttpRequest object exists. You can do this by including this code before creating the XMLHttpRequest instance.
84+
85+
{% highlight coffeescript %}
86+
if (typeof @XMLHttpRequest == "undefined")
87+
console.log 'XMLHttpRequest is undefined'
88+
@XMLHttpRequest = ->
89+
try
90+
return new ActiveXObject("Msxml2.XMLHTTP.6.0")
91+
catch error
92+
try
93+
return new ActiveXObject("Msxml2.XMLHTTP.3.0")
94+
catch error
95+
try
96+
return new ActiveXObject("Microsoft.XMLHTTP")
97+
catch error
98+
throw new Error("This browser does not support XMLHttpRequest.")
99+
{% endhighlight %}
100+
101+
This code ensures the XMLHttpRequest object is available in the global namespace.
95102

96-
Line 13 sends our request to the server.

0 commit comments

Comments
 (0)