【发布时间】:2016-09-30 09:14:50
【问题描述】:
我已安装 v2.0.1 的 Confluent 数据平台,并且已启动并运行 Kafka Connect 服务。在尝试从 Backbone.js 框架发出 REST 调用时,我得到了 CORS 异常(跨域异常)。我不依赖主干同步 API,因为我对 Kafka Connect 使用普通的 jquery AJAX 调用 ($.ajax)。像这样的调用:
$.ajax({
type: 'GET',
url: 'http://<host>:8083/connectors',
crossDomain:true,
dataType: 'json',
async: true,
success: function(data, status, xhr){
Messenger.trigger('...', data);
},
error: function (xhr, ajaxOptions, thrownError) {
Messenger.trigger('...', thrownError);
}
})
导致 CORS 异常。我试过 dataType: 'jsonp' 然后我得到一个 200 响应代码但仍然调用错误函数。该应用程序是一个 100% 在浏览器中运行的 Javascript 应用程序,由 nginx 网络服务器提供服务。一切都在同一台机器上运行 - 使调用“跨域”的唯一部分是端口号:连接 8083 和应用程序端口 80。
注意:如果我从 Iceweasel 以 'http://localhost:8083' 的形式发出 GET,我会得到“正确”的响应,并且“使用完全相同”的 AJAX 会导致以下响应:
http://localhost:8083/connectors
GET /connectors HTTP/1.1
Host: localhost:8083
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.2.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost/index.htm
Origin: http://localhost
Connection: keep-alive
HTTP/1.1 200 OK
Date: Wed, 01 Jun 2016 08:38:36 GMT
Content-Type: application/json
Content-Length: 30
Server: Jetty(9.2.12.v20150709)
但是,触发错误功能而不是成功。有趣的是,Content-length 是 30,这是生成的 JSON 数组 [<string1>, <string2>] 中可用的字符数量。
【问题讨论】:
标签: jquery ajax rest cors apache-kafka