我正在使用 Streamsets 管道从浏览器流式传输数据。因此,为此,我创建了一个具有 HTTP 服务器源的管道,以从浏览器 Javascript 发布数据,并尝试使用 REST 客户端写入该 URL,并成功写入。响应标头已在 SDC.properties 文件中设置。
http.access.control.allow.origin=*
http.access.control.allow.headers=origin, content-type, accept, authorization, x-requested-by, x-ss-user-auth-token, x-ss-rest-call
http.access.control.allow.methods=GET, POST, PUT, DELETE, OPTIONS, HEAD
但是当我尝试使用XMLHTTPRequest从 JavaScript 写入一些数据时,它会为飞行前请求引发错误。下面是 JavaScript 代码:
var http = new XMLHttpRequest();
var value = '{ "prop1": "value 2", "prop2": "value 2" }';
var url ='http://13.68.93.97:8100/'
var async = true
if ('withCredentials' in http) {
http.open('OPTIONS', url, async);
} else if (typeof XDomainRequest != "undefined") {
http = new XDomainRequest(); //for IE
http.open('OPTIONS', url);
} else {
http.open('OPTIONS', url, async);
}
http.open('POST', url, true);
http.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
http.setRequestHeader('X-SDC-APPLICATION-ID', 'testApp1');
http.setRequestHeader("Access-Control-Allow-Origin", "*");
http.setRequestHeader('Access-Control-Allow-Methods', "POST, OPTIONS, GET");
http.setRequestHeader('Access-Control-Allow-Credentials', "true");
http.setRequestHeader('Access-Control-Expose-Headers', "X-Region");
http.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Accept-language");
http.withCredentials = true;
http.onreadystatechange = function () {
if (http.readyState == 4 && http.status == 200) {
console.log(http.responseText);
}
}
http.send(value);
上述代码执行引发的错误:
XMLHttpRequest 无法加载http://13.68.93.97:8100/。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问源“ http://localhost ”。
任何帮助都会非常有帮助。谢谢..!!