0

I'm building an Rails app and I'm trying to do an AJAX call with jQuery. When I do the AJAX call (POST), I receive an error from my method. But I don't know what the problem is :(

Everything worked when I'm using the 'GET' method, but when I change it to 'POST', I'm receiving the error (and yes, I changed the route.rb :) )

jQuery

$.ajax({
  url: "/login/checkLogin/",
  dataType: "json",
  data: data,
  type: 'post',
}).done(function(response){
  console.log(response);
})

Method

ldap = Net::LDAP.new
ldap.host = "***"
ldap.port = ***
ldap.auth %[#{params[:login_username]}], params[:login_password]

if ldap.bind
 session['loggedin'] = true
 respond_with "1".to_json
else
 session['loggedin'] = false
 respond_with "0".to_json
end

The error message (coming from the console)

undefined method `"0"_url'

Thanks

2 Answers 2

3

It is backside of Rails magic :) By default, Rails uses ActionController::Responder to process respond_with if no block given or template is not available. For 'POST' method (REST 'create') and non-html formats like xml and json, it generates a response with encoded resource, status code (:success), and resource location. So it tries to find out the URI of the resource you have passed, and fails on this step.

Just pass :location => nil option to override defaults, or use explicit respond_to block

respond_with "0".to_json, :location => nil

or

respond_to do |format|
  format.json {render :json => "0".to_json}
end
Sign up to request clarification or add additional context in comments.

Comments

0

Try putting contentType: "application/json; charset=utf-8", for $.ajax({...})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.