0

I have a url that returns either a file or, on error, a JSON response. When the request is acceptable, it responds with a file and the user should be prompted for download (as one might expect in that situation). When there's a problem with the request, the response is a JSON object which should be processed further via javascript.

The request is made via javascript (AJAX, opened in new tab, generated iframe, whatever). How might I go about handling both kinds of responses from the same url via javascript?


The strategy I'm current tinkering with (haven't got working yet) is:

  1. Generate an iframe
  2. Inject a form into the iframe
  3. Set the error event handler on the iframe
  4. Trigger submission of the form (I have control over the response headers so I can use Content-Disposition: attachment to force the save dialog.)
  5. If the error event handler is triggered, scrap the iframe for the json response

Turns out error events aren't fired by iframes and load events aren't fired in certain browsers when the iframe is pointed to a file that triggers a save dialog.

Ouroborus
  • 16,237
  • 4
  • 39
  • 62
  • on error you get Function( jqXHR jqXHR, String textStatus, String errorThrown ) regardless of the type response for success. so shouldn't be an issue – Bryan Dellinger Feb 09 '17 at 01:23
  • @BryanDellinger That's true. However, on success the user should be prompted to download the supplied file. What I'd like to know is how to submit the response to the user for saving (as you might with a normal download) while still catching the possible error response of the same request. – Ouroborus Feb 09 '17 at 01:28
  • got it, similiar question here http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery which suggests using the html5 download attribute – Bryan Dellinger Feb 09 '17 at 01:31
  • @BryanDellinger Doesn't seem to indicate any way to catch an error response. – Ouroborus Feb 09 '17 at 01:35
  • so I just did this a few months back on success I used jquery append to put the links for the download in a div and I handled the error the normal way. – Bryan Dellinger Feb 09 '17 at 01:38
  • @BryanDellinger That would cause two hits. One to check for an error and another to do the actual download. I'm trying to avoid this since the error would be caused if there were a problem with the file generation and I don't want to cache or regenerate the file on the server so that it could be made available for the second attempt. – Ouroborus Feb 09 '17 at 01:45
  • true, thats the way mine ended up worked. one to generate the label/s (they are shipping labels) and then one to go get them off of the server. unfortunately as the answer below suggested I was unable to validate client side as most of the processing happened on a mainframe. – Bryan Dellinger Feb 09 '17 at 01:53

1 Answers1

1

Before hitting that url, it appears that in your situation, some parameters must be present in order for your function call to prompt you back the file.

Write a function that first checks the data you are serializing for that url. This function can either make a call to the url if the data you are serializing passes validation or return the error JSON response if the data you are serializing fails validation.

Validation of data before serialization can be done on client side with javascript, or on the server side. Ideally, you want validation on both client and server.

d1du
  • 296
  • 1
  • 3
  • 12