我的客戶端應用程序將JSON編碼的POST發送到rails服務器,但服務器顯示406錯誤並且不響應json。已完成406在773ms內不可接受
UsersController創建
# POST /users
# POST /users.json
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
Rails的控制檯:
Started POST "/users.json" for 127.0.0.1 at 2013-05-21 16:38:47 +0100
Processing by Devise::RegistrationsController#create as JSON
Parameters: {"user"=>{"name"=>"", "available"=>"true", "email"=>"", "sex"=>"male", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
WARNING: Can't verify CSRF token authenticity
(0.2ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '' LIMIT 1
(0.2ms) rollback transaction
Completed 406 Not Acceptable in 773ms (ActiveRecord: 6.1ms)
誰能幫助我?
我建議的一件事是關閉CSRF令牌的真實性。爲此,請轉至呈現json的控制器並添加'skip_before_filter:verify_authenticity_token'。像調整其他過濾器一樣調整。這當然假設你無法在將JSON發送到Rails的應用程序中生成CSRF令牌。 – jason328
關閉[跨網站請求僞造保護](https://en.wikipedia.org/wiki/Cross-site_request_forgery)對我來說聽起來不是一個好主意。 –