If you get the following extra HTML headers after using the jQuery AJAX method on a page and immediately clicking on another link:
0
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
Content-Type: text/html; charset=utf-8
X-UA-Compatible: IE=Edge
Transfer-Encoding: chunked
Tracing back to my webapp, it looks like the error was due to jQuery expecting a callback from my Rails AJAX method. It times out automatically after a few seconds, but if you click on another link immediately after it will generate additional HTML headers. To fix this, I added a new response to the script as a JSON.
In my Rails app, I added the following line to the corresponding method in the controller:
render json: nil, status: :ok
Ruby 3+
I didn’t have to make any additional changes on the jQuery script. The following worked for me without any issue:
$.ajax({
url : "/myapp/myajaxscript",
type: "POST",
data : formData,
success: function(data,status)
{
console.log(status);
}
});