2017-02-09 16 views
1

我正在使用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'。

任何幫助將非常有幫助。謝謝..!!

回答

1

StreamSets Data Collector當前不支持HTTP Server origin中的CORS - sdc.properties文件中的那些屬性是針對SDC Web UI的。我已經提交了Jira,SDC-5298,但我很好奇你的用例。您是僅僅使用瀏覽器進行實驗,還是您看到用戶通過JavaScript向SDC發送數據的生產用例?

如果您有生產用例,請對該Jira留言,我們將看看如何將CORS添加到HTTP Server源。

如果你只是想辦法輕鬆地發送數據時,你有幾個選擇:

從命令行
  1. 使用捲曲:

    curl http://localhost:8000/ -H 'X-SDC-APPLICATION-ID: bob' -d '{"a":"b"}' 
    
  2. 安裝Chrome擴展,如CORS Toggle在Chrome中禁用CORS檢查。

  3. 使用--disable-web-security選項啓動Chrome。

相關問題