In rails I’ve got a form with remote true in it like so:
<%= form_for(@thing, :html => { :'data-type' => 'json' }, :remote => true ) do |f| %>
And I’m picking up the ajax event in the javascript with this:
$('#new_thing').bind('ajax:complete', function(json, status, xhr){
console.log("complete!");
console.log(json);
console.log(xhr);
console.log(status);
});
In the controller I have the line of code to respond to it like this:
respond_to do |format|
format.json { render :json => @thing }
end
The problem is the console logged return response from the server isn’t a json object, it’s status object and the json response is a text string under the property “responseText” which has been escaped.
How do I get the server to respond with just a json object of the @thing?
Is there something wrong in any of the stages in this?
Thanks!
Try
parseJSON(json['responseText'])to get JSON object of servers response. I think you should manually bind Ajax call on form submit event, to get JSON object without conversions insuccesshandler of$.ajax()function