【问题标题】:Rails JSON requestRails JSON 请求
【发布时间】:2014-01-13 17:35:03
【问题描述】:

我遇到了一个问题,我的 rails v4 应用程序无法识别 JSON 请求。我正在从我的移动/客户端应用程序发送以下标头('Content-Type' => 'application/json'),并且请求有效负载是 JSON 字符串。

Started POST "/devices" for 127.0.0.1 at 2013-12-25 17:50:46 -0800
Processing by DevicesController#create as */*

respond_to do |format|
  format.html
  format.json do
    # Request does not come in this block
  end
end

但是,当我将“.json”附加到我的请求 URL 时,我的 rails 应用程序能够将其作为 JSON 请求进行处理

Started POST "/devices.json" for 127.0.0.1 at 2013-12-25 17:13:43 -0800
Processing by DevicesController#create as JSON

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails json ruby-on-rails-4


    【解决方案1】:

    所以Content-Type 标头描述的是请求的类型,而不是响应的类型。

    Rails 从 HTTP 接受中确定所需的响应格式 客户端提交的header。

    您可以做的是检查request.content_typerender json: {} 或相应的HTML。

    【讨论】:

      猜你喜欢
      • 2016-05-06
      • 1970-01-01
      • 2010-11-17
      • 2014-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      相关资源
      最近更新 更多