【问题标题】:sinatra sending headers and bodysinatra 发送标题和正文
【发布时间】:2014-08-01 19:47:00
【问题描述】:

我正在通过网络联系表向 sinatra 发帖。效果很好。我需要设置自定义标头,因为它是一个 CORS 请求,它也很完美。

但是当我想向 :status 和 :message 的请求返回一个回复 json 时,我似乎发送了一个 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

【问题讨论】:

    标签: ruby sinatra


    【解决方案1】:

    问题解决了

    删除:

    response[:status] = "error"
    response[:message] = "No email"
    

    添加:

    {:status => "error", :message => "No email"}.to_json
    

    它有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-12
      • 2011-09-05
      • 2018-12-04
      • 2017-08-31
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多