javascript - SetInterval Form Submit with AJAX Rails -
i want .edit_canvas
submit every 5 seconds via ajax. right seems nothing happening. have no console errors though.
javascript:
setinterval(function(){ // html content of canvas element var canvascontent = $('.canvas').html(); // populate hidden field canvas content $('#canvas-content').val(canvascontent); // submit form ajax $('.edit_canvas').submit(function() { var valuestosubmit = $(this).serialize(); $.ajax({ url: $(this).attr('action'), //sumbits given url of form type: 'post', data: valuestosubmit, datatype: "json" // want difference between normal , ajax-calls, , json standard }).success(function(json){ $('.canvas').css('background', 'pink'); }); // prevent normal behaviour return false; }); }, 5000);
form:
<%= form_for @canvas |f| %> <%= f.hidden_field :content, value: "", id: "canvas-content" %> <%= f.submit "save", id: "save-canvas" %> <% end %>
what you're doing attaching submit event handler, not submitting form via ajax.
remove submit code , should work
setinterval(function(){ // html content of canvas element var canvascontent = $('.canvas').html(); // populate hidden field canvas content $('#canvas-content').val(canvascontent); // submit form ajax var valuestosubmit = $('.edit_canvas').serialize(); $.ajax({ url: $('.edit_canvas').attr('action'), //sumbits given url of form type: 'post', data: valuestosubmit, datatype: "json" // want difference between normal , ajax-calls, , json standard }).success(function(json){ $('.canvas').css('background', 'pink'); }); }, 5000);
Comments
Post a Comment