angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

$http responseError not called when response body is empty (only in PhantomJS)

Open oniisanlabs opened this issue 12 years ago • 8 comments

If I receive an HTTP response with the status code 429 and an empty response body the responseError method of my HTTP interceptor is not called. This bug occurs only in PhantomJS, Chrome works well. If the response contains valid JSON also PhantomJS works.

Does anybody have a clue whether this is an angular or a phantom problem?

oniisanlabs avatar Aug 13 '14 15:08 oniisanlabs

angular doesn't care if there's a response or not, but it's possible that their version of webkit transforms "no body" responses into an error?

It warrants investigation, please provide some code to reproduce this

caitp avatar Aug 13 '14 15:08 caitp

Alright, I will see what I can do to provide a code example.

oniisanlabs avatar Aug 14 '14 12:08 oniisanlabs

Alright the setup for the example was a bit more effort. Because you need a node.js backend to provide the 429 response I couldn't use plunkr and had to pack things into a zip archive. You can download the example at: http://www73.zippyshare.com/v/70060075/file.html. If you don't like zippyshare I also set up a plunkr where you can download the files as a zip archive (http://plnkr.co/edit/5gD7cB1yokLaw8yqBxYm?p=preview).

After downloading and unpacking you will find a server.js which simply contains a node.js server. After starting this server with "node server.js" you will have a webserver running on localhost:1337 that is only delivering 429 response status codes.

Now if you open the index.html in your browser (e.g. Chrome) and click the "Test response error" button you will see the text "Successfully executed responseError callback". So in Chrome everything works fine. But if you run "phantomjs phantom-test.js" you will see that a file named "output.png" is rendered. This file will contain a screenshot not showing the expected text we have already seen in Chrome. And exactly this is my problem. I need phantomJS to accept the empty 429 response and run the responseError callback of my HTTP interceptor.

If you need any further details or can't reproduce the problem just let me know.

oniisanlabs avatar Aug 15 '14 17:08 oniisanlabs

Okay, I've looked into this, I'm pretty sure you have 2 issues here.

  1. you're using a version of jQuery which is too old for angular (angular requires jQuery.fn.on() to be implemented
  2. you're probably not triggering angular's click handler from your phantomjs script, because you loaded jQuery after angular was loaded. Load jQuery from the main index.html, before angular, instead.

Fix those things, and you should get the response handler to work (I certainly did)

caitp avatar Aug 15 '14 18:08 caitp

  1. The example also works with jquery version 1.6.1 (tested in Chrome). But of course you are right I should haved used a newer jquery version so I exchanged 1.6.1 with 2.1.1 in my plunkr code.
  2. Angular's click handler is triggered - the code runs fine in every browser except of Phantom.
  3. If you run the example with Phantom but replace the line res.end(''); with res.end('{}'); in the file server.js you will see that the error callback is executed successfully. So this why it's definitely a problem that occurs due to the response body (empty vs valid JSON).

oniisanlabs avatar Aug 25 '14 10:08 oniisanlabs

@caitp Would be great if you'd find a moment for trying to reproduce the things I mentioned in my last comment.

oniisanlabs avatar Aug 27 '14 13:08 oniisanlabs

The reason index.html "works in Chrome" is because you aren't loading jQuery in index.html.

The empty response is not the issue, I was able to make your reproduction work simply by correcting programming errors. The default response interceptor does not care if your response is an empty string or not.

Follow the instructions above and the code works in phantomjs 1.9.2

edit actually, maybe the empty response is an issue, I can't remember if I modified that too at the time. Who can say. But from the code, there's no reason we'd care if there's an empty response or not.

caitp avatar Aug 27 '14 13:08 caitp

Alright, maybe it might be a bug in PhantomJS. Just to make sure that is has got nothing to do with any jQuery version I removed jQuery from the phantom script in this plunkr. Now the AJAX request is triggered 100ms after the page load so no button click is necessary anymore.

http://plnkr.co/edit/pCwYQC324ceeW8C0gOg0?p=preview

If you have no other idea I would raise an issue for the phantom crew.

oniisanlabs avatar Aug 27 '14 15:08 oniisanlabs