簡體   English   中英

跨域發布Javascript

[英]Cross Domain post Javascript

所以我正在嘗試制作一個網頁,通過客戶端啟動本地應用程序。

我能想到的簡單解決方案就是將數據發布或獲取到我想要使用的端口; 說端口3000.不幸的是我遇到了一個錯誤: XMLHttpRequest cannot load %3127.0.0.1:3000. Cross origin requests are only supported for HTTP. 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);
}
}

是的,附帶條件。 您的第一個問題是您必須使用HTTP或HTTPS。 你需要一個URI方案(或者你需要用//來啟動它來維護當前的方案,如果你遇到一個特定的端口,這是不明智的)。

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

在端口3000上運行的服務器需要使用CORS授予站點權限。 用戶還必須配備支持CORS瀏覽器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM