Skip to content

Commit e414422

Browse files
AndrewLaneflovilmart
authored andcommitted
Provide a visual message of what went wrong when Ajax requests fail (#283)
1 parent 8842e83 commit e414422

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

public/assets/css/style.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ a:visited {
197197
color: #fff !important;
198198
}
199199

200+
.step--error {
201+
color: red;
202+
font-weight: bold;
203+
}
204+
200205
#prod-test {
201206
margin-bottom: 60px;
202207
}

public/assets/js/script.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ Steps.fillStepOutput = function(id, data) {
3333
$(id).html('Output: ' + data).slideDown();
3434
}
3535

36+
Steps.fillStepError = function(id, errorMsg) {
37+
$(id).html(errorMsg).slideDown();
38+
}
39+
40+
3641
Steps.fillBtn = function(id, message) {
3742
$(id).addClass('success').html('✓ ' + message);
3843
}
@@ -62,9 +67,12 @@ ParseRequest.postData = function() {
6267
ParseRequest.getData();
6368
e.preventDefault();
6469
});
65-
});
70+
},
71+
function(error) {
72+
Steps.fillStepError('#step-1-error', 'There was a failure: ' + error);
73+
});
6674
XHR.POST('/parse/classes/GameScore');
67-
}
75+
};
6876

6977
ParseRequest.getData = function() {
7078
XHR.setCallback(function(data){
@@ -77,10 +85,13 @@ ParseRequest.getData = function() {
7785
Steps.bindBtn('#step-3-btn', function(e){
7886
ParseRequest.postCloudCodeData();
7987
e.preventDefault();
80-
})
81-
});
88+
});
89+
},
90+
function(error) {
91+
Steps.fillStepError('#step-2-error', 'There was a failure: ' + error);
92+
});
8293
XHR.GET('/parse/classes/GameScore');
83-
}
94+
};
8495

8596
ParseRequest.postCloudCodeData = function() {
8697
XHR.setCallback(function(data){
@@ -90,7 +101,10 @@ ParseRequest.postCloudCodeData = function() {
90101
Steps.fillBtn('#step-3-btn', 'Tested');
91102
// open third step
92103
Steps.showWorkingMessage();
93-
});
104+
},
105+
function(error) {
106+
Steps.fillStepError('#step-3-error', 'There was a failure: ' + error);
107+
});
94108
XHR.POST('/parse/functions/hello');
95109
}
96110

@@ -120,12 +134,16 @@ Config.getUrl = function() {
120134

121135
var XHR = {};
122136

123-
XHR.setCallback = function(callback) {
137+
XHR.setCallback = function(callback, failureCallback) {
124138
this.xhttp = new XMLHttpRequest();
125139
var _self = this;
126140
this.xhttp.onreadystatechange = function() {
127-
if (_self.xhttp.readyState == 4 && _self.xhttp.status >= 200 && _self.xhttp.status <= 299) {
128-
callback(_self.xhttp.responseText);
141+
if (_self.xhttp.readyState == 4) {
142+
if (_self.xhttp.status >= 200 && _self.xhttp.status <= 299) {
143+
callback(_self.xhttp.responseText);
144+
} else {
145+
failureCallback(_self.xhttp.responseText);
146+
}
129147
}
130148
};
131149
}

public/test.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
</div>
4040
</div>
4141
</div>
42+
<div id="step-1-error" class="hidden pure-u-4-5 step--error"></div>
4243
</div>
4344

4445
<div id="step-2" class="pure-g step--container step--disabled">
@@ -56,6 +57,7 @@
5657
</div>
5758
</div>
5859
</div>
60+
<div id="step-2-error" class="hidden pure-u-4-5 step--error"></div>
5961
</div>
6062

6163
<div id="step-3" class="pure-g step--container step--disabled">
@@ -73,6 +75,7 @@
7375
</div>
7476
</div>
7577
</div>
78+
<div id="step-3-error" class="hidden pure-u-4-5 step--error"></div>
7679
</div>
7780

7881
<div id="step-4" class="pure-g step--container hidden">

0 commit comments

Comments
 (0)