Skip to content

Commit 9c9df33

Browse files
committed
Added Ajax Without jQuery recipe.
1 parent 438cb4d commit 9c9df33

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
layout: recipe
3+
title: AJAX Request Without jQuery
4+
chapter: AJAX
5+
---
6+
## Problem
7+
8+
You want to load data from your server via AJAX without using the jQuery library.
9+
10+
## Solution
11+
12+
You will use the native <a href="http://en.wikipedia.org/wiki/XMLHttpRequest" target="_blank">XMLHttpRequest</a> object.
13+
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+
45+
Let's set up a simple test HTML page with a button.
46+
47+
{% highlight html %}
48+
<!DOCTYPE HTML>
49+
<html lang="en-US">
50+
<head>
51+
<meta charset="UTF-8">
52+
<title>XMLHttpRequest Tester</title>
53+
</head>
54+
<body>
55+
<h1>XMLHttpRequest Tester</h1>
56+
<button id="loadDataButton">Load Data</button>
57+
58+
<script type="text/javascript" src="XMLHttpRequest.js"></script>
59+
</body>
60+
</html>
61+
{% endhighlight %}
62+
63+
Let's finish our XMLHttpRequest.coffee by adding a 'click' event listener then create our XMLHttpRequest object.
64+
65+
{% highlight coffeescript linenos %}
66+
loadDataFromServer = ->
67+
req = new XMLHttpRequest()
68+
69+
req.addEventListener 'readystatechange', ->
70+
if req.readyState is READYSTATE_COMPLETE
71+
if req.status is 200 or req.status is 304
72+
data = eval '(' + req.responseText + ')'
73+
console.log 'data message: ', data.message
74+
else
75+
console.log 'Error loading data...'
76+
77+
req.open 'GET', 'data.json', false
78+
req.send()
79+
80+
loadDataButton = document.getElementById 'loadDataButton'
81+
loadDataButton.addEventListener 'click', loadDataFromServer, false
82+
{% endhighlight %}
83+
84+
## Discussion
85+
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.
87+
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.
89+
90+
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.
91+
92+
The last thing we need to do is actually make our request.
93+
94+
Line 12 opens a 'GET' request to retreive the data.json file.
95+
96+
Line 13 sends our request to the server.

0 commit comments

Comments
 (0)