Ruby on rails RubyonRails和POST,内容类型:text/plain

Ruby on rails RubyonRails和POST,内容类型:text/plain,ruby-on-rails,curl,Ruby On Rails,Curl,嘿,伙计们(长期潜伏者) 当我提交一个内容类型为text/plain的POST请求时,我有一个关于rails在做什么的问题 我的问题是我试图通过JQuery的AJAX(使用XDomainRequest,强制内容类型为text/plain)在IE 8中提交一些内容,但rails没有接收提交的数据,下面是rails IE的一个示例Curl请求: curl-ihttps://localhost:8080/v1/auth -H'内容类型:文本/普通'-d'登录=test@example.com“-d”p

嘿,伙计们(长期潜伏者)

当我提交一个内容类型为text/plain的POST请求时,我有一个关于rails在做什么的问题

我的问题是我试图通过JQuery的AJAX(使用XDomainRequest,强制内容类型为text/plain)在IE 8中提交一些内容,但rails没有接收提交的数据,下面是rails IE的一个示例Curl请求:

curl-ihttps://localhost:8080/v1/auth -H'内容类型:文本/普通'-d'登录=test@example.com“-d”password=testpassword123”--不安全

于2014-08-28 14:09:21+1000为127.0.0.1发布了“/v1/auth”
由V1::AuthController#创建为进行处理*/*
..
在1ms内完成401次未授权(ActiveRecord:0.2ms)

正如您所看到的,Rails没有获取登录名或密码。这里是相同的curl请求,但内容类型设置为x-www-form-urlencoded

curl-ihttps://localhost:8080/v1/auth -H'内容类型:应用程序/x-www-form-urlencoded'-d'登录=test@example.com“-d”password=testpassword123”--不安全

于2014-08-28 14:42:46+1000为127.0.0.1发布了“/v1/auth”
由V1::AuthController#创建为进行处理*/*
参数:{“登录”=>“test@example.com,“password”=>“testpassword123”}
....
(0.8ms)提交事务
在1624ms内完成200 OK(视图:0.5ms |活动记录:3.3ms)


另外,如果我评估请求对象,我就看不到任何关于我所追求的东西。我是不是走错了路,还是我完全错过了什么?谢谢大家

如您所述,XDomainRequests要么发送“text/plain”内容类型,要么根本不发送内容类型头。rails依赖于将出现的“x-www-form-urlencoded”内容类型头来将帖子正文解析为表单数据。要使用rails处理XDomainRequest post请求,可以手动解析原始post正文

为此,我创建了一个助手:

# correctly parses data from an IE8/IE9 XDomainRequest POST, which doesn't have a
# content type.
def cross_origin_params
  if request.content_type == 'text/plain' || request.content_type.blank?
    parsed_post_body = Rack::Utils.parse_nested_query(request.raw_post)
    parsed_post_body_params = ActionController::Parameters.new(parsed_post_body)
    parsed_post_body_params.deep_merge(params)
  else
    params
  end
end
在访问请求数据时,我引用了
cross_origin_params
,而不是
params