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 :(
我正在构建一个Rails应用程序,我正在尝试使用jQuery进行AJAX调用。当我执行AJAX调用(POST)时,我从我的方法收到错误。但我不知道问题是什么:(
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 :) )
当我使用'GET'方法时,一切正常,但当我将其更改为'POST'时,我收到错误(是的,我更改了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 个解决方案
#1
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.
它是Rails magic的背面:)默认情况下,如果没有给定的块或模板不可用,Rails使用ActionController :: Responder来处理respond_with。对于'POST'方法(REST'create')和非html格式(如xml和json),它会生成带有编码资源,状态代码(:success)和资源位置的响应。所以它试图找出你传递的资源的URI,并在这一步失败。
Just pass :location => nil
option to override defaults, or use explicit respond_to
block
只需传递:location => nil选项覆盖默认值,或使用显式的respond_to块
respond_with "0".to_json, :location => nil
or
respond_to do |format|
format.json {render :json => "0".to_json}
end
#2
0
Try putting contentType: "application/json; charset=utf-8",
for $.ajax({...})
尝试为$ .ajax({...})添加contentType:“application / json; charset = utf-8”
#1
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.
它是Rails magic的背面:)默认情况下,如果没有给定的块或模板不可用,Rails使用ActionController :: Responder来处理respond_with。对于'POST'方法(REST'create')和非html格式(如xml和json),它会生成带有编码资源,状态代码(:success)和资源位置的响应。所以它试图找出你传递的资源的URI,并在这一步失败。
Just pass :location => nil
option to override defaults, or use explicit respond_to
block
只需传递:location => nil选项覆盖默认值,或使用显式的respond_to块
respond_with "0".to_json, :location => nil
or
respond_to do |format|
format.json {render :json => "0".to_json}
end
#2
0
Try putting contentType: "application/json; charset=utf-8",
for $.ajax({...})
尝试为$ .ajax({...})添加contentType:“application / json; charset = utf-8”