出于某种原因,当使用json或xml向我的应用程序发出请求时,我得到一个InvalidAuthenticityToken.我的理解是rails应该只为html或js请求提供真实性令牌,因此我不应该遇到这个错误.到目前为止,我发现的唯一解决方案是禁用protect_from_forgery来执行我想通过API访问的任何操作,但由于显而易见的原因,这并不理想.思考?
def create
respond_to do |format|
format.html
format.json{
render :json => Object.create(:user => @current_user, :foo => params[:foo], :bar => params[:bar])
}
format.xml{
render :xml => Object.create(:user => @current_user, :foo => params[:foo], :bar => params[:bar])
}
end
end
Run Code Online (Sandbox Code Playgroud)
每当我将请求传递给操作时,这就是我在日志中获得的内容:
Processing FooController#create to json (for 127.0.0.1 at 2009-08-07 11:52:33) [POST]
Parameters: {"foo"=>"1", "api_key"=>"44a895ca30e95a3206f961fcd56011d364dff78e", "bar"=>"202"}
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
thin (1.2.2) lib/thin/connection.rb:76:in `pre_process'
thin (1.2.2) lib/thin/connection.rb:74:in `catch'
thin (1.2.2) lib/thin/connection.rb:74:in `pre_process'
thin (1.2.2) lib/thin/connection.rb:57:in `process'
thin (1.2.2) lib/thin/connection.rb:42:in `receive_data'
eventmachine (0.12.8) lib/eventmachine.rb:242:in `run_machine'
eventmachine (0.12.8) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,允许通过点击直接标记照片(如Facebook,flickr等).但是,我似乎无法为照片上的点击注册正确的坐标.问题是x坐标似乎是浏览器窗口内(而不是照片内)点击的绝对x距离,而y坐标通常是负的或非常小(靠近顶部的负值,靠近底部的小值).这些是我在左上角附近点击时得到的值(应该注册为0或者接近0:"x"=>"219","y"=>" - 311"...... 219似乎是正确的测量距浏览器窗口左侧的距离,但距离应在照片区域内)
我目前正在使用常规链接(链接包含其他相关照片数据)捕获照片上的点击事件和坐标,并在将其传递给我的rails应用程序之前进行数学运算(jquery文档中使用的相同计算).我怀疑这个方法与错误的值有很大关系,尽管我怀疑数学或某些css怪可能有问题.在任何一种情况下,我都绝对是难以置信的.
JS:
$(document).ready(function(){
clickTag();
Run Code Online (Sandbox Code Playgroud)
});
function clickTag(){
$("#taggable").click(function(e){
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
var url = $(this).attr("href");
courl = url + '&x=' + x + '&y=' + y;
$.ajax({
type:"GET",
url: courl,
dataType:"script"
});
return false;
});
}
Run Code Online (Sandbox Code Playgroud)
CSS:
`<div class="content">
<div id="image_container" style="position:relative;width:405px;float:left;height:600px;>
<a href="/tags/new_xy?look_id=188&photo_id=1150" id="taggable" style="position:relative;top:0;left:0px;"><img alt="taggable_image" src="taggable_image.jpg" /></a>
<div class="tags" id="tags"></div>
</div>
</div>`
Run Code Online (Sandbox Code Playgroud)