问题描述
我正在从客户端向Sinatra发送AJAX请求,但是不知为何数据没有显示.Chrome请求标头选项卡表明在客户端一切正常:
I am making AJAX request from the client to Sinatra but somehow the data doesn't show up.Chrome request headers tab suggests that on the client side is everything OK:
Request Payload
{ test: Data }
但是,在Sinatra一侧
However, on Sinatra's side
post '/api/check/:name' do
sleep 3
puts params.inspect
end
和控制台:
127.0.0.1 - - [03/Feb/2014 10:45:53] "POST /api/check/name HTTP/1.1" 200 17 3.0019
{"splat"=>[], "captures"=>["name"], "name"=>"name"}
找不到发布数据,怎么了?
Post data is nowhere to be found, what's wrong ?
推荐答案
这是常见的错误.Sinatra只是解析表单数据(源).
It's a common fault. Sinatra just parse form data (source).
要解决此问题,请使用 rack-contrib
或 request.body
.
To fix this use rack-contrib
or the request.body
.
Form参数如下所示
Form parameter would look like this
curl -X POST 127.1:4567/ -d "foo=bar"
您可以仅使用 request.body.read
或使用rack contrib代替参数.
Instead of params you can just use request.body.read
or use rack contrib.
机架控制
- 使用
gem install rack-contrib
安装 -
要求
- Install it with
gem install rack-contrib
Require it
需要机架/容器"
加载它使用Rack :: PostBodyContentTypeParser
使用此方法,您可以像往常一样将 params
用于json发布数据.像这样:
With this you can use params
as normal for json post data. Something like this:
curl -X POST -H "Content-Type: application/json" -d '{"payload":"xyz"}' 127.1:4567/
此来源:
Source for this:
这篇关于Sinatra不显示POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!