Sinatra没有发送标题

时间:2014-07-16 12:55:52

标签: jquery sinatra cors thin

我无法使用thin / sinatra发送标头。我正在尝试做一个CORS请求。我试着尽可能简单。

我尝试了sinatra cors模块。我尝试自己添加标题,如下所示。当我使用ngnix并从那里强制标题时,我得到了它。但我试图理解为什么从应用程序发送标题不起作用...

› gem list sinatra

*** LOCAL GEMS ***

sinatra (1.4.5)

contact.rb

require 'sinatra'
require 'json'

options '/*' do
  p "options hit"
  headers  "Access-Control-Allow-Headers" => "origin, x-requested-with, content-type"
    status 200
end

post '/' do
  p params.inspect
  headers \
    "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"}.to_json
end

**all.js**

    // Contact form submission
    $(document).ready(function() {

        $('#contactForm').submit(function() {

            $.ajax({
                type: "POST",
                url: "http://localhost:4567/",
                data: $("#contactForm").serialize(),
                dataType: "json",

                success: function(msg) {
                    $("#formResponse").removeClass('error');
                    $("#formResponse").removeClass('success');
                    $("#formResponse").addClass(msg.status);
                    $("#formResponse").html(msg.message);

                },
                error: function() {
                    $("#formResponse").removeClass('success');
                    $("#formResponse").addClass('error');
                    $("#formResponse").html("There was an error submitting the form. Please try again.");
                }
            });
        });
    });

输出

    › ruby contact.rb
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.2 codename Doc Brown)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
"options hit"
127.0.0.1 - - [21/Jul/2014 11:34:59] "OPTIONS / HTTP/1.1" 200 - 0.0059

浏览器请求

Remote Address:127.0.0.1:80
Request URL:http://cooldevops.dev/
Request Method:POST
Status Code:405 Method Not Allowed
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,nl;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:16
Content-Type:application/x-www-form-urlencoded
Host:cooldevops.dev
Origin:http://cooldevops.dev
Referer:http://cooldevops.dev/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Form Dataview sourceview URL encoded
email:
mailbody:
Response Headersview source
Connection:keep-alive
Content-Length:19
Content-Type:text/plain
Date:Wed, 16 Jul 2014 12:47:06 GMT
X-Cascade:pass

修改

我使用safari / chrome获取错误。使用curl时似乎有效。所以它变得更加奇怪。

2.0.0-p247 in gtheys/
› curl -v -X POST http://localhost:4567
* Adding handle: conn: 0x7f8efc815000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f8efc815000) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 4567 (#0)
*   Trying ::1...
*   Trying fe80::1...
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4567 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:4567
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST
< Access-Control-Allow-Headers: Content-Type
< Access-Control-Max-Age: 86400
< Content-Length: 39
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Connection: keep-alive
* Server thin 1.6.2 codename Doc Brown is not blacklisted
< Server: thin 1.6.2 codename Doc Brown
<
* Connection #0 to host localhost left intact
{"status":"error","message":"no email"}%

2 个答案:

答案 0 :(得分:1)

这不是CORS问题。但我糟糕的jquery实现。所以请确保你不要复制它!

答案 1 :(得分:0)

根据Setting Body, Status Code and Headers上的文档:

  

与正文相似,您还可以设置状态代码和标题:

get '/foo' do
  status 418
  headers \
    "Allow"   => "BREW, POST, GET, PROPFIND, WHEN",
    "Refresh" => "Refresh: 20; http://www.ietf.org/rfc/rfc2324.txt"
  body "I'm a tea pot!"
end

headers 而不是response.headers

您还可以通过块中的响应值设置标题,请参阅Return Values