2012-05-14 92 views
0

所以我試圖做一個網頁,通過客戶端啓動本地應用程序。Cross Domain post Javascript

我能想到的simplist解決方案只是發佈或獲取數據到我想要使用的端口;說端口3000.不幸的是我遇到了一個錯誤:XMLHttpRequest cannot load %3127.0.0.1:3000. Cross origin requests are only supported for HTTP.

我的問題: 有沒有辦法使用XMLHttpRequest()發佈或獲取數據到127.0.0.1:3000?

對格式混亂對不起,我打得四處很長一段時間,我不能讓它正確地縮進d:

function Post_Command(Command_String){ 
if(typeof(Command_String) != "string"){ 
console.log(
    "Invalid Call:\Post_Command(\n\t"+Command_String+":"+typeof(Command_String)+"\n)" 
); 
} else { 
    xmlhttp=new XMLHttpRequest(); 
    xmlhttp.onreadystatechange=function(){ 
     if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
      console.log(xmlhttp.responseText); 
     } 
    } 
    xmlhttp.open(
     "POST", 
     "127.0.0.1:3000", 
     true 
    ); 
    xmlhttp.send(Command_String); 
} 
} 

回答

2

是的,有附加條件。你的第一個問題是你必須使用HTTP或HTTPS。你需要在你的URI上有一個方案(或者你需要用//來啓動它以保持當前的方案,如果你正在打一個特定的端口,這是不明智的)。

"127.0.0.1:3000" --> "http://127.0.0.1:3000" 

運行在端口3000上的服務器需要使用CORS來授予該站點的權限。用戶還必須配備browser that supports CORS

+0

這可能是我正在尋找。它也不會是一個接收數據的瀏覽器,它只是一個客戶端應用程序,它可以解析接收到的數據。我也會寫這個。 – Ignoreme