I’m trying to replace the content of a div after clicking on a link using Rails 3, remote_link :remote => true and jQuery.
So far, I’ve been able to get the controller render the correct partial while responding with a 200 HTTP code. I’ve set some callbacks to find the origin of the problem:
jQuery(function($) {
$("#follow-link").bind("ajax:before", function() {
console.log("ajax:before");
});
$("#follow-link").bind("ajax:success", function(data, status, xhr) {
console.log("ajax:success");
});
$("#follow-link").bind("ajax:complete", function() {
console.log("ajax:complete");
});
$("#follow-link").bind("ajax:error", function(xhr, status, error) {
console.log("ajax:error");
console.log(error);
});
});
While before and complete are triggered, success is not and error outputs “parsererror”. The content I get when I inspect the response in Safari’s developers tools is a simple string.
Why would it raise a parsererror? How can I get more information about what’s causing this error?
I’m going to propose an answer because comments don’t allow for any formatting. Here is is: Something is happening on the server side and jQuery is not getting what you think it is. Here’s an excerpt from the jQuery documentation:
That implies that your controller may be responding with something other than the expected data. In that controller, try:
In any case, check your logs to make sure what you think is happening really is happening. Also, write a test to verify this:
Ok, it’s a hacky, brittle spec, but it will do until you know where things are going wrong.
Once you get this working, everything else will fall neatly into place.