我正在使用Streamsets管道從瀏覽器傳輸數據。爲此,我創建了一個HTTP服務器源的管道,以便從瀏覽器的Javascript中發佈數據,並嘗試使用REST客戶端在該URL中寫入數據,併成功寫入。響應頭文件已經在SDC.properties文件中設置。將數據發佈到SDC來自JavaScript的HTTP服務器URL顯示CORS問題
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'。
任何幫助將非常有幫助。謝謝..!!