我是从网络联系表发布到sinatra的。哪个效果很好。我需要设置自定义标头,因为它是一个CORS请求,它也很完美。
但是当我想要回复json请求:status和:message时,我似乎发送了一个sinatra对象而不是正确的json。
此外,如果我在日志中看到打印输出,则消息不在正文中,而是在标题中。
代码片段:
headers \
"Access-Control-Allow-Origin" => "*",
"Access-Control-Allow-Methods" => "POST",
"Access-Control-Allow-Headers" => "Content-Type",
"Access-Control-Max-Age" => "86400"
if params[:email].empty?
response[:status] = "error"
response[:message] = "No email"
p response.inspect
body response.to_json
elsif params[:mailbody].empty?
response[:status] = "error"
response[:message] = "No message?"
body response.to_json
elsif
...
日志文件:
"#<Sinatra::Response:0x007fc62bfed2f8 @status=200, @header={\"Content-Type\"=>\"application/json\", \"Access-Control-Allow-Origin\"=>\"*\", \"Access-Control-Allow-Methods\"=>\"POST\", \"Access-Control-Allow-Headers\"=>\"Content-Type\", \"Access-Control-Max-Age\"=>\"86400\", :status=>\"error\", :message=>\"No email\"}, @chunked=false, @writer=#<Proc:0x007fc62bfecf88@/Users/gtheys/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/response.rb:27 (lambda)>, @block=nil, @length=0, @body=[]>"
127.0.0.1 - - [01/Aug/2014 10:48:57] "POST / " 200 39 0.0077
答案 0 :(得分:0)
问题解决了
删除:
response[:status] = "error"
response[:message] = "No email"
添加:
{:status => "error", :message => "No email"}.to_json
它有效