Jquery Sinatra不发送头

Jquery Sinatra不发送头,jquery,sinatra,thin,Jquery,Sinatra,Thin,我无法使用thin/sinatra发送标题。我正试着做一个CORS的请求。我尽可能地简单 我试过sinatra cors模块。我试着自己添加标题,如下所示。当我使用ngnix并从那里强制执行头文件时,它就开始工作了。但是我试图理解为什么从应用程序发送头不起作用 › gem list sinatra *** LOCAL GEMS *** sinatra (1.4.5) contact.rb require 'sinatra' require 'json' options '/*' do

我无法使用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"}%
gtheys中的
2.0.0-p247/
›卷曲-v-X柱http://localhost:4567
*添加句柄:conn:0x7f8efc815000
*正在添加句柄:发送:0
*正在添加句柄:recv:0
*卷曲度加把手尺寸线:长度:1
*-Conn 0(0x7f8efc815000)发送管道:1,接收管道:0
*即将连接()到本地主机端口4567(#0)
*正在尝试::1。。。
*正在尝试fe80::1。。。
*正在尝试127.0.0.1。。。
*已连接到本地主机(127.0.0.1)端口4567(#0)
>POST/HTTP/1.1
>用户代理:curl/7.30.0
>主机:本地主机:4567
>接受:*/*
>
根据以下文件:

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

它是
标题
而不是
响应。标题


您还可以通过块的响应值设置标题,请参见。

问题不在于CORS。但是我糟糕的jquery实现。所以一定不要复制它

这是我第一次尝试的方法之一:)它产生了同样的问题:HTTP/1.1 405方法不允许。也许这是一个Rack::Protection设置,那么,看看这些答案是否有用。我已经尝试过了。但我只有在使用浏览器时才会出错。当我使用curl时,它可以正常工作…@gtheys这个Chrome扩展被列入白名单的版本怎么样?搜索并尝试了我在google/stackoverflow上能找到的一切。但是继续调试,我认为我发现了问题。问题不在西纳特拉。但是我使用anvil for mac来运行静态网站。此Web服务器似乎在sinatra发回回复之前关闭了连接。我将尝试使用另一个Web服务器作为带有联系人表单的静态站点。
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