【发布时间】:2011-08-15 10:55:49
【问题描述】:
我有一个通过移动应用程序访问的 Rails 应用程序 - 数据以 JSON 格式交换。
当我执行成功的 POST 时,我期望并得到一个 HTTP 代码 200 OK 返回。我没想到的是伴随的 1 字节数据 ASCII 0x20(即一个空格)。
在被 POST 的对象(设备)已经存在的情况下,我有以下代码从 POST 返回。
# Device is already registered, so update attributes of existing record (incl. device token)
if @deviceFound.update_attributes(params[:device])
format.html { redirect_to(@deviceFound, :notice => 'Device was successfully updated.') }
format.xml { head :ok }
# format.json { head :ok }
format.json do
render :nothing => true, :status => :ok
return true
end
else
format.html { render :action => "new" }
format.xml { render :xml => @deviceFound.errors, :status => :unprocessable_entity }
format.json { render :json => @deviceFound.errors, :status => :unprocessable_entity }
end
从注释掉的行中,你会看到我一直在使用 format.json { head :ok } 但为了理解为什么我会返回这个字节,我尝试了我的替代实现相信是等价的。两者产生相同的结果 HTTP 200 + 1 字节数据。
顺便说一句,如果我在这种情况下过滤掉 1 个字节,在所有其他情况下,我的移动应用程序与 Rails 应用程序交互都很好。
如果有人能解释为什么我在响应中得到一个字节的数据,我将不胜感激?
谢谢。
【问题讨论】:
-
这是为了解决 Safari 中的一个错误。看看stackoverflow.com/questions/3351247/…
-
啊!谢谢,这就解释了。如果您想添加您的评论作为答案,我会这样标记。再次感谢。
标签: ruby-on-rails http http-headers