1

我正在使用 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 似乎没问题。

4

1 回答 1

4

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

有关更多信息,请参阅此帖子: 无法在 JQuery 1.6.4 中使用 CORS 进行 PUT/POST/DELETE HTTP 调用

于 2012-12-04T21:14:16.010 回答