Http 204 Status Code leads to Chrome downloading a File

Question: How do you solve the issue of Google Chrome downloading a file when you return a status code of 204 "meaning No Content". Chrome should not download a file when you specifically return a 204 status code.

 


Please Login to see the rest of the answer

Answer: I would like to know why this behavior happens in Chrome, however, if I had to give it a thought, I would conclude that Chrome would not know how to handle 204 Response code. If you handle the response message in a Call Back function Chrome doesn't download the file. However, you could work around it.

  1. Install jquery-ajax-unobtrusive library
  2. Attach or link the library to your HTML file to be able to use the Javascript
  3. Create a link in HTML5 and specify to use data-ajax="true"
  4. Then, specify data-ajax-method="post"
  5. Go ahead and specify the data-ajax-url="MyCoolUrl"

    The code is below
     <a data-ajax="true" data-ajax-method="post" data-ajax-success="MyCallBackJavaScriptFunction" data-ajax-url="MyCoolUrl">
                        Click Me
     </a>?
  6. The 204 Http Response is then handled in the Call Back function defined in the data-ajax-success, this makes Chrome think not to do anything as there is a Call Back function to handle the Response.

Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy