I’m having a very weird situation that is driving me nuts. I have a rails app that sends a simple string to jQuery and prints it out. Here is my code:
jQuery:
jQuery(document).ready(function() {
jQuery('.bid a').livequery('click', function() {
obj = jQuery(this);
jQuery.post(obj.attr('href'), function(data) {
alert(data)
});
return false;
});
})
Rails Controller:
def send_message
respond_to do |format|
format.js do
@res_str = "nobids"
render :json => @res_str
end
end
end
Like that, it works ok, it shows an alert with the string “nobids”. However, if I want to send any other string, lets say “nobidss”, it fails! It makes no sense at all!
What could be happening?
I was making a rookie mistake!
If you render :json you have to convert the response message to json, ie: @response.to_json.
Also, in the html, the href has to end in .json, and in the controller you have to respond to format.json too.
I cannot tell why it was succeeding with the string “nobids” anyways.
It works beautifull now