我有以下 HTML 代码。
<html>
<head>
<script>
function someFunction() {
var strWindowFeatures = "location=yes,height=570,width=520,scrollbars=yes,status=yes";
var win = window.open(URL, "_blank", strWindowFeatures);
win.document.write(UserAction());
win.document.close();
}
function UserAction() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "myUrl", false);
xhttp.setRequestHeader("Authorization", "Basic " + Base64.encode("myId:myPwd"));
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send();
var response = JSON.parse(xhttp.responseText);
console.log(response);
return response;
}
</script>
</head>
<body>
<input type="button" onclick="someFunction()" />
</body>
</html>
这里,当我单击按钮时,它会打开一个新窗口,并显示错误,如下图所示。
当我看到 console.log()
时,我在新的(弹出)窗口中看到如下消息。
Not allowed to load local resource: file:///C:/Users/rakesh.keerthi/Desktop/nodePractice/AlexaNode/function%20URL()%20%7B%20[native%20code]%20%7D
在我的实际 html 页面中,我得到的错误为
sample.html:17 XMLHttpRequest cannot load file:///C:/Users/rakesh.keerthi/Desktop/nodePractice/AlexaNode/myUrl. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. UserAction @ sample.html:17 someFunction @ sample.html:8 onclick @ sample.html:26 sample.html:17 Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/Users/rakesh.keerthi/Desktop/nodePractice/AlexaNode/myUrl'. at UserAction (file:///C:/Users/rakesh.keerthi/Desktop/nodePractice/AlexaNode/sample.html:17:19) at someFunction (file:///C:/Users/rakesh.keerthi/Desktop/nodePractice/AlexaNode/sample.html:8:32) at HTMLInputElement.onclick (file:///C:/Users/rakesh.keerthi/Desktop/nodePractice/AlexaNode/sample.html:26:53)
当我在 postman 中尝试同样的事情时,它起作用了,无法理解我哪里出错了。请让我知道如何解决这个问题。
谢谢
最佳答案
如果您使用的是 google chrome,您可以尝试使用以下命令运行它
--allow-file-access-from-files
switch enabled
关于javascript - 无法使用 javascript 获取其余响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46299019/