使用ajax和jquery获得rails 3.1
我知道这是一个愚蠢的问题,有非常相似的问题,但没有一个真正解决我的问题:使用ajax和jquery获得rails 3.1,jquery,ruby-on-rails,ajax,content-type,Jquery,Ruby On Rails,Ajax,Content Type,我知道这是一个愚蠢的问题,有非常相似的问题,但没有一个真正解决我的问题: #application.js //= require jquery //= require jquery_ujs //= require_tree . #form <%= form_for @comments, :remote => true, do |comment| %> ..... <% end %> #controller def create @comments = Com
#application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
#form
<%= form_for @comments, :remote => true, do |comment| %>
.....
<% end %>
#controller
def create
@comments = Comment.new params[:posts]
if @posts.save
respond_to do |format|
format.html {render 'something'}
format.js {render :content_type => 'text/javascript', :layout => false}
end
end
#app/view/comment/create.js.erb
alert("hello world, please work!");
我发现有人通过添加charset:content\u type=>'text/javascript;charset=UTF-8'会有帮助,但它没有魔力
我也尝试将其添加到application.js中,但也没有什么神奇之处
$(function() {
$.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
}
});
});
谁能给我指点一下吗?顺便说一句,我的Webbrick服务器中的错误显示了406个不可接受的错误。我真的很沮丧。我怀疑的是我做得不对的内容类型…尝试将数据类型属性设置为表单
参考:还可以查看下一页的数据类型参数。它不起作用。。。我只是编辑了{:'data-type'=>'脚本,:remote=>true,do | comment |%>但它仍然直接响应html行,我甚至尝试删除format.html行,只剩下format.js,但它仍然没有做任何好的输入:'data-type'=>'脚本'我确实尝试了这个,但也不起作用..,我做错了什么吗?jQuery.ajax{url:/post/create cache:true,dataType:script,success:functiondata{alerthello;}}首先,在上面的代码中,我们不需要在缓存之前加一个逗号吗。请查看rails服务器日志,检查您是以html还是js/script类型获取请求。
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" even I set the content_type to be text/javascript.
$(function() {
$.ajaxSetup({
'beforeSend': function(xhr) {
xhr.setRequestHeader("Accept", "text/javascript");
}
});
});