CORS:总是通过jQuery.ajax从Mojolicious :: Lite获得空响应

时间:2012-11-25 15:17:36

标签: jquery cors mojolicious mojolicious-lite

我正在使用jQuery从我正在构建的Mojolicious::Lite API中获取一些数据。但我无法通过Ajax从API接收任何数据。我可以通过curl获取数据。

更新:我可以在同一个域上通过Ajax获取数据。这是一个CORS问题。

这是我的客户代码:

$.ajax({
  type: 'POST',
  url:  'http://localhost:3000/path',
  data: JSON.stringify({ foo: 'foo', bar: 'bar' }),
  dataType: 'json',
  contentType: 'application/json',
  beforeSend: function(xhr){
    xhr.setRequestHeader('Accept', 'application/json');
  }
});

以下是API代码:

#!/usr/bin/perl -w

use Mojolicious::Lite;
use strict;

options '*' => sub {
  my $self = shift;

  $self->res->headers->header('Access-Control-Allow-Origin'=> 'http://localhost:7000');
  $self->res->headers->header('Access-Control-Allow-Credentials' => 'true');
  $self->res->headers->header('Access-Control-Allow-Methods' => 'GET, OPTIONS, POST, DELETE, PUT');
  $self->res->headers->header('Access-Control-Allow-Headers' => 'Content-Type, X-CSRF-Token');
  $self->res->headers->header('Access-Control-Max-Age' => '1728000');

  $self->respond_to(any => { data => '', status => 200 });
};

post '/path' => sub {
  return $_[0]->render( json => {hello => "world"} );
};

app->start;

以下是所有请求的标题和数据:

OPTIONS /path

Request headers:

OPTIONS /path HTTP/1.1
Host: localhost:3000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20.0 Firefox/20.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pt-br,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Origin: http://localhost:7000
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Response headers:

HTTP/1.1 200 OK
Connection: keep-alive
Access-Control-Allow-Headers: Content-Type, X-CSRF-Token
Access-Control-Allow-Credentials: true
X-Powered-By: Mojolicious (Perl)
Date: Sun, 25 Nov 2012 14:45:19 GMT
Access-Control-Allow-Origin: http://localhost:7000
Access-Control-Allow-Methods: GET, OPTIONS, POST, DELETE, PUT
Content-Length: 0
Access-Control-Max-Age: 1728000
Content-Type: text/html;charset=UTF-8
Server: Mojolicious (Perl)

POST /path

Request headers:

POST /path HTTP/1.1
Host: localhost:3000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:20.0) Gecko/20.0 Firefox/20.0
Accept: application/json
Accept-Language: pt-br,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Content-Type: application/json; charset=UTF-8
Referer: http://localhost:7000/
Content-Length: 24
Origin: http://localhost:7000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

Response headers:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: application/json
X-Powered-By: Mojolicious (Perl)
Date: Sun, 25 Nov 2012 14:45:19 GMT
Content-Length: 17
Server: Mojolicious (Perl)

Request data: {"foo":"foo","bar":"bar"}
Response data:

不知道问题是否在我的JS或Perl代码上。

更新:可能在JS代码上。 更新 CORS似乎没问题。

1 个答案:

答案 0 :(得分:4)

非常确定您还需要在Allow-*响应中包含与CORS相关的POST标头,而不仅仅是OPTIONS

有关其他信息,请参阅此帖子:Unable to make PUT/POST/DELETE HTTP Call using CORS in JQuery 1.6.4