我通过twitter引导模式窗口传递ajax调用来更新应用程序中的数据。ajax代码如下所示:
  $(document).ready(function(){
    var link=$('#link_hash').val();
  $("#update_link").click(function(){
    console.log("I'm here");
     $.ajax({
            url: "profiles/update_link",
            type: "POST",
            dataType: "html",
            data: {link: link,data: $('#link_hash').val() },
            success: function(data) {
              // some code
            },
            error: function(data1) {
              // some code
             }
            });
        });
      });我修改了route.rb文件,使其与控制器"update_link“方法相匹配。我的方法中的代码如下:
  def update_link
    @link=Link.find_by_link(params[:link])
    @tlink=Link.find_by_link(params[:data])
    logger.info "=========kkkkkkkkkkkkkk=================================#{@link.inspect}"
    logger.info "=========kkkkkkkkkkkkkk=================================#{@tlink.inspect}"
    logger.info "=========kkkkkkkkkkkkkk=================================#{params.inspect}"
    respond_to do |format|
      if @tlink.nil? 
         @link.update_attributes(:link => params[:data])
        ...some code....
      else
    ...some code...
      end
    end
  end
end所以在服务器日志中显示-
Started POST "/profiles/update_link" for 127.0.0.1 at 2013-02-20 12:08:20 +0530
Processing by ProfilesController#update_link as HTML
  Parameters: {"link"=>"9bfzjp", "data"=>"9bfzjpaaa"}
WARNING: Can't verify CSRF token authenticity
Completed 401 Unauthorized in 6ms所以很明显,"logger.info"并没有显示up...Now在搜索后我能够解决的警告,但仍然有401 present...How来解决这个问题吗??
提前谢谢..。
发布于 2013-02-20 15:22:23
根据您的服务器日志,您没有传递CSRF令牌,因此rails自动认为请求是恶意的,并将其标记为未验证的请求。未验证请求的默认处理是重置会话。您可以注释掉protect_from_forgery或将skip_before_filter :verify_authenticity_token添加到您的控制器中以查看是否是这样吗?
如果希望在ajax请求中包含真实性令牌(强烈建议),可以将其添加到ajax请求的标题中:
headers: {
      'X-Transaction': 'POST Example',
      'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
}发布于 2016-05-20 03:21:58
将skip_before_filter :authenticate_user!添加到控制器中。
https://stackoverflow.com/questions/14975489
复制相似问题