title: Ruby on Rails $.ajax({dataType: 'script'}) form submission with respond_to format.js and format.json
body: <p>i have a form submitting via ajax.  if form successfully submits, the page redirects elsewhere. if the form fails i rerender the form with partials (.js.erb) and inline errors.</p>
  
  <p>this works fine.</p>
  
  <p>the issue im having is i want to have to send some type of json response/data back to application.js when the page rerenders.  Only, im not sure if this is possible.</p>
  
  <p>//application.js</p>
  
  <pre><code>$(document).on('click','#sumbit_div',function(e) {
  $.ajax({
     url: '/create',
     type: 'post',
     dataType: 'script',
     data: $('#the_form').serialize(),
     cache: false,
     success: function(xhr,data) {
       console.log('success');
     },
     error: function(data) {
       console.log('error')
       console.log(data.value_from_format_json);
     },
  });
  });
  </code></pre>
  
  <p>//create action in controller.</p>
  
  <pre><code>rescue ActiveRecord::RecordInvalid =&gt; e
  
  respond_to do |format|
  format.json {render :json =&gt; { :status =&gt; 403,:success =&gt; false }}  
  format.js
  end
  </code></pre>
  
  <p>I recognize that even a failed submission is a success in the .ajax response. i can deal with how to handle this after.  Right now i'd just like to know if i can accomplish what im after.</p>
  
  <p>last note is this is on rails 2.3 (still in the process of upgrading).  But i have all jquery libs (rails.js/jquery.js/etc) update to work with ujs.</p>
  
  <p>thanks</p>