javascript - 如何在 AJAX 请求中忽略/避免“自签名证书错误”?
问题描述
我正在尝试向使用自签名证书的服务器发出发布请求,但这样做时出现自签名证书错误。有没有办法避免或使 JavaScript 代码忽略此类错误?
<script type="text/javascript">
function sendToServer(){
var username, password;
username = "";
password = "";
var xhr;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 8 and older
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//creating the xml string
xmlString = "";
var url = "";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/vnd.emc.apollo-v1+xml");
xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
xhr.send(xmlString);
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
}
</script>
解决方案
自签名证书不受浏览器信任,因为它们是由发出请求的同一服务器生成的。所以你必须配置你的浏览器来信任证书。如果您使用的是 chrome,请按照下列步骤操作:
在浏览器中访问该网站
单击地址栏中的“查看站点信息”图标(挂锁):
点击“证书信息”。
在详细信息选项卡上,单击“复制到文件”并使用默认值保存文件。
找到并双击刚刚保存的证书文件。
在证书常规选项卡上,单击“安装证书”。
在安装证书向导中,选择“将所有证书放入以下存储”。
单击“浏览”并选择“受信任的根证书颁发机构”,然后继续执行其余步骤以安装证书。
推荐阅读
- c - 将用户输入字符串打印到最小的用户输入文件中
- mongodb - Mongo-Kafka 源连接器无法恢复更改流
- email - 根据选择选择案例以通过电子邮件发送不同的工作表
- django-rest-framework - 每当应用搜索时增加模型的计数字段
- javascript - 数组在 for 循环中使用 try catch 工作?(node.js 和 javascript)我收到错误“ReferenceError:p 未定义”
- mongodb - 为什么 mongodb arbiter 需要 dbpath?
- r - 删除两个文本之间的第一位逗号和空格以及最后一个逗号或空格
- python - 堆叠一个 numpy 数组列表
- c++ - Boost property_tree 错误:在 .ini 文件中获取元素时,将数据转换为“j”类型失败
- javascript - 在 Angular 9 中分配表单值