0

I have a problem when I send an AJAX request with jquery in my Rails 4 app (ruby 2.0).

The error is:

ERROR Errno::ECONNRESET: Connection reset by peer

JS:

$("#user_main_filter_all").click(function() 
{
    $("#spinner").show();
$.ajax({
        dataType: "json",
  cache: false,
        url: '/finder/main_filter_changed/all',
  timeout: 5000,
        beforeSend : function(xhr) { xhr.setRequestHeader("Accept", "application/json") },
  error: function(XMLHttpRequest, errorTextStatus, error) { alert("Failed to submit : "+ errorTextStatus+" ;"+error); },
  success: function(data) 
        {                    
            $("#user_main_filter_all").css("background-color","#000000");
            $("#user_main_filter_stored").css("background-color","transparent");

            $("#spinner").hide();

            refresh_user_data();
    }
});
});

Ruby (method called):

  def main_filter_changed
    session[:user_page] = nil

    if !params[:id].blank?
      session[:user_main_filter] = params[:id]
    else
      session[:user_main_filter] = 'all'
    end

    respond_to do |format|
      format.json  { render :json => nil }     
    end

  end

The method is called and just after, I have in my logs:

Processing by FinderController#main_filter_changed as JSON
  Parameters: {"_"=>"1387190059468", "id"=>"all"}
Completed 200 OK in 3ms (Views: 0.2ms | ActiveRecord: 0.5ms)
[2013-12-16 11:34:20] ERROR Errno::ECONNRESET: Connection reset by peer

    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:80:in `eof?'
    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/httpserver.rb:80:in `run'
    /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

My first though was a problem with protect_from_forgery and csrf_token... but I just tested and commented the line protect_from_forgery and I have the same behaviour.

So the server error gives the javascript alert:

error: function(XMLHttpRequest, errorTextStatus, error) { alert("Failed to submit : "+ errorTextStatus+" ;"+error); },

is called the the alert box shown (but not the errorTextStatus).

alex.bour
  • 2,842
  • 9
  • 40
  • 66
  • Is that the whole stack-trace? does your app run at all? can it access the database? – phoet Dec 16 '13 at 13:00
  • Yes, the app can access to the database. If the click "OK" on the JS alert (failed to submit), the refresh_user_data(); method is called... and all ruby code works successfully... except the error alert. – alex.bour Dec 16 '13 at 13:17
  • Sorry. I ran jquery .click(function() AND the normal link_to on my image. So Rails was unable to understand what I would have. I remove the link_to and all is OK now. – alex.bour Dec 16 '13 at 16:11

0 Answers0