我正在向我的服务器运行一个简单的 AJAX 发布,respond_to
.js block 作为 200 成功返回,但未执行。我的配置或执行返回 js 代码的代码中是否缺少某些内容?我正在使用 Ruby on Rails 3.2.9。
JavaScript:
$("#test-submit").on("click",function(event){
$.ajax({
type: "POST",
url: '/users',
dataType: 'json',
data: {
"test" : "test-data"
}
});
event.preventDefault();
});
Controller 操作:
def create
user = User.find(current_user)
respond_to do |format|
format.js { }
end
end
用户create.js.erb:
alert('hai');
最佳答案
...
dataType: 'json',
...
您需要 JSON 数据,而不是可执行脚本。尝试将此行更改为
dataType: 'script',
关于jquery - 为什么在 Ruby on Rails 中没有使用 jQuery AJAX 执行 respond_to js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13579867/