0

Let suppose we have a dialog with some settings, then on dialog button "OK" the settings are sent to a controller function via AJAX call. This call either result a downloadable file, or an error described in a (partial) html source code. In latter case this error message must be inserted into the dialog.

$.ajax( { 
   url: url, 
   data: dialogFormData, 
   success: function(data) { ???? }
});

How to handle this situation? How to handle the ajax response to process the result? Any idea?

Zoltan Hernyak
  • 989
  • 1
  • 14
  • 35

1 Answers1

2

What you want is to check the response type and act accordingly.
You can try something like:

$.ajax({
  url: url, 
  data: dialogFormData, 
  success: function(response, status, xhr){ 
    var ct = xhr.getResponseHeader("content-type") || "";
    if (ct.indexOf('multipart/form-data') > -1) {
      // do something with file
    }
    if (ct.indexOf('text/html') > -1) {
      // do something with html
    } 
  }
});

I didn't test this code, you may need to play around with the types to get the correct ones.

iamdlm
  • 1,885
  • 1
  • 11
  • 21