javascript - 无法使用 javascript 获取其余响应

标签 javascript html

我有以下 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>

这里,当我单击按钮时,它会打开一个新窗口,并显示错误,如下图所示。

enter image description here

当我看到 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/

相关文章:

javascript - Express >4.0 版本的 Socket.io 和 Node.js

javascript - 如何在数据更新时更新可重用的 d3.js 图形?

javascript - 如何覆盖 CSS 配色方案设置

html - Webkit 3D 透视错误

javascript - 尝试使用函数在 div 中插入图像

javascript - D3 图表可以在 jsfiddle 上完美运行,但不能在 localhost 上运行

javascript - 为什么 isNaN ("")(带空格的字符串)等于假?

javascript - 如何优化 Jquery 中的 ajax 请求?

javascript - 在 Node.js 中使用 readline

javascript - 如何通过 HTML 表单输入文本并使用 Javascript 从 JSON 文件中检索数据作为响应?