我正在使用 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;
以下是所有请求的 header 和数据:
选项/路径
:
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/路径
:
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 似乎没问题。
最佳答案
非常确定您需要包含与 CORS 相关的 Allow-*
POST
中的标题也有回应,而不仅仅是 OPTIONS
.
请参阅此帖子以获取更多信息:Unable to make PUT/POST/DELETE HTTP Call using CORS in JQuery 1.6.4
关于jquery - CORS:始终通过 jQuery.ajax 从 Mojolicious::Lite 获得空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552337/