From 216686696712d4a3cfe1ebe9b43c3fb5c8e2dd30 Mon Sep 17 00:00:00 2001 From: BehindTheMath Date: Thu, 15 Mar 2018 16:12:32 -0400 Subject: [PATCH] Handle XHR response error (#137) * Move the XHR callback to a separate file * Trigger an error event if the response cannot be parsed. * Add tests for handle-response.js --- README.md | 30 ++++- index.js | 63 +-------- lib/proto/handle-response.js | 70 ++++++++++ lib/send-request.js | 8 +- tests/lib/proto/handle-response.js | 204 +++++++++++++++++++++++++++++ 5 files changed, 310 insertions(+), 65 deletions(-) create mode 100644 lib/proto/handle-response.js create mode 100644 tests/lib/proto/handle-response.js diff --git a/README.md b/README.md index 5e25114..6b9a833 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,34 @@ pjax.loadUrl("/your-url") pjax.loadUrl("/your-other-url", {timeout: 10}) ``` +#### `handleResponse(responseText, request, href)` + +This method takes the raw response, processes the URL, then calls `pjax.loadContent()` to actually load it into the DOM. + +It is passed the following arguments: + +* **responseText** (string): This is the raw response text. This is equivalent to `request.responseText`. +* **request** (XMLHttpRequest): This is the XHR object. +* **href** (string): This is the URL that was passed to `loadUrl()`. + +You can override this if you want to process the data before, or instead of, it being loaded into the DOM. + +For example, if you want to check for a JSON response, you could do the following: + +```js +var pjax = new Pjax(); + +pjax._handleResponse = pjax.handleResponse; + +pjax.handleResponse = function(responseText, request, href) { + if (request.responseText.match("