Skip to content

Commit 5cbbfe8

Browse files
Greg Jacobsondomenic
authored andcommitted
Enhance requestOkText example.
1 parent 4f5a18d commit 5cbbfe8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,12 +547,12 @@ function requestOkText(url) {
547547
if (request.status === 200) {
548548
deferred.resolve(request.responseText);
549549
} else {
550-
onerror();
550+
deferred.reject(new Error("Status code was " + request.status));
551551
}
552552
}
553553

554554
function onerror() {
555-
deferred.reject("Can't XHR " + JSON.stringify(url));
555+
deferred.reject(new Error("Can't XHR " + JSON.stringify(url)));
556556
}
557557

558558
function onprogress(event) {
@@ -563,6 +563,21 @@ function requestOkText(url) {
563563
}
564564
```
565565

566+
Below is an example of how to use this ``requestOkText`` function:
567+
568+
```javascript
569+
requestOkText("http://localhost:3000")
570+
.then(function (responseText) {
571+
// If the HTTP response returns 200 OK, log the response text.
572+
console.log(responseText);
573+
}, function (error) {
574+
// If there's an error or a non-200 status code, log the error.
575+
console.error(error);
576+
}, function (progress) {
577+
// Log the progress as it comes in.
578+
console.log("Request progress: " + Math.round(progress * 100) + "%");
579+
});
580+
```
566581

567582
### The Middle
568583

0 commit comments

Comments
 (0)