Skip to content

Commit 07f559d

Browse files
committed
Merge branch 'ajax-non-jquery'
2 parents 438cb4d + c943705 commit 07f559d

File tree

6 files changed

+110
-6
lines changed

6 files changed

+110
-6
lines changed

authors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The following people are totally rad and awesome because they have contributed r
1919
* João Moreno *coffeecb @joaomoreno .com*
2020
* Jeff Pickhardt *pickhardt (at) gmail (dot) com*
2121
* Frederic Hemberger
22+
* Mike Hatfield *[email protected]*
2223
* ...You! What are you waiting for? Check out the [contributing](/contributing) section and get cracking!
2324

2425
# Developers
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
Let's set up a simple test HTML page with a button.
15+
16+
{% highlight html %}
17+
<!DOCTYPE HTML>
18+
<html lang="en-US">
19+
<head>
20+
<meta charset="UTF-8">
21+
<title>XMLHttpRequest Tester</title>
22+
</head>
23+
<body>
24+
<h1>XMLHttpRequest Tester</h1>
25+
<button id="loadDataButton">Load Data</button>
26+
27+
<script type="text/javascript" src="XMLHttpRequest.js"></script>
28+
</body>
29+
</html>
30+
{% endhighlight %}
31+
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.
42+
43+
{% highlight coffeescript linenos %}
44+
# XMLHttpRequest.coffee
45+
loadDataFromServer = ->
46+
req = new XMLHttpRequest()
47+
48+
req.addEventListener 'readystatechange', ->
49+
if req.readyState is 4 # ReadyState Compelte
50+
if req.status is 200 or req.status is 304 # Success result codes
51+
data = eval '(' + req.responseText + ')'
52+
console.log 'data message: ', data.message
53+
else
54+
console.log 'Error loading data...'
55+
56+
req.open 'GET', 'data.json', false
57+
req.send()
58+
59+
loadDataButton = document.getElementById 'loadDataButton'
60+
loadDataButton.addEventListener 'click', loadDataFromServer, false
61+
{% endhighlight %}
62+
63+
## Discussion
64+
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.
68+
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.
72+
73+
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.
74+
75+
The last thing we need to do is actually make our request.
76+
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.
102+

chapters/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Functions
1212
- Metaprogramming
1313
- jQuery
14+
- Ajax
1415
- Regular Expressions
1516
- Networking
1617
- Design Patterns
@@ -38,4 +39,4 @@ <h2><a href="{{ url }}">{{ chapter }}</a></h2>
3839
</li>
3940

4041
{% endfor %}
41-
</ul>
42+
</ol>

chapters/math/random-integer.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ randomInt = (lower, upper=0) ->
2525

2626
(randomInt(1, 10) for i in [0...10])
2727
# => [7,3,9,1,8,5,4,10,10,8]
28+
{% endhighlight %}
2829

2930
## Discussion
3031

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Functions
1212
- Metaprogramming
1313
- jQuery
14+
- Ajax
1415
- Regular Expressions
1516
- Networking
1617
- Design Patterns
@@ -42,4 +43,4 @@ <h2><a href="{{ url }}">{{ chapter }}</a></h2>
4243
</li>
4344

4445
{% endfor %}
45-
</ul>
46+
</ol>

wanted-recipes.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ evens.every even
4747

4848
## Dates and Times
4949

50-
* Calculating the phase of the moon
51-
* Number of days between two dates
50+
* Empty
5251

5352
## Math
5453

@@ -107,8 +106,7 @@ foo 1, 2, 3
107106

108107
* Creational Patterns
109108
* Abstract Factory
110-
* Prototype
111-
* Singleton
109+
* Prototype
112110

113111
* Structural Patterns
114112
* Adapter

0 commit comments

Comments
 (0)