gpt4 book ai didi

javascript - 跨域发布 Javascript

转载 作者:行者123 更新时间:2023-12-02 19:39:22 25 4
gpt4 key购买 nike

所以我正在尝试制作一个网页,通过客户端启动本地应用程序。

我能想到的最简单的解决方案就是将数据发布或获取到我想要使用的端口;比如说端口 3000。不幸的是,我遇到了一个错误:XMLHttpRequest 无法加载 %3127.0.0.1:3000。仅 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 向站点授予权限。用户还必须配备browser that supports CORS .

关于javascript - 跨域发布 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10586595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com