我有js文件:
$('#some_btn').click(function() {
var valuesToSubmit = $('#some_form').serialize();
var url = $('#some_form').attr('action');
console.log("VALUE: " + valuesToSubmit);
console.log("URL: " + search_url);
$.ajax({
type: 'POST',
url: url, //sumbits it to the given url of the form
data: valuesToSubmit,
dataType: "JSON",
success: function(data) {
console.log("saved");
console.log(data);
}
});
return false;
});
Controller Action 响应:
def some_action()
...
@response = {resp: "ack"}
respond_with @response do |format|
format.json { render :layout => false, :text => @response }
end
end
路线:
post '/abc/some_action', to: 'abc#some_action'
但是执行后,我收到:
ArgumentError
Nil location provided. Can't build URI.
@response = {resp: "ack"}
respond_with @response do |format| # <--- Error here
format.json { render :layout => false, :text => @response }
end
最佳答案
respond_with
期望可以从中推断出路线的AR对象。
更改:
@response = {resp: "ack"}
respond_to do |format|
format.json { render json: @response }
format.js { render json: @response }
end
一种替代方法是强制 Controller 仅针对特定操作呈现json。很奇怪,因为这意味着您无法发送正确的请求。
但是在这种情况下:
respond_to :json, :only => :some_action
在您的操作中:
render json: @response
关于ruby-on-rails - “Nil location provided. Can' t建立URI的方式。”在rails中执行AJAX请求时意味着什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18981668/