首页 > 解决方案 > 如何在 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>

标签: javascriptajax

解决方案


自签名证书不受浏览器信任,因为它们是由发出请求的同一服务器生成的。所以你必须配置你的浏览器来信任证书。如果您使用的是 chrome,请按照下列步骤操作:

  1. 在浏览器中访问该网站

  2. 单击地址栏中的“查看站点信息”图标(挂锁):

  3. 点击“证书信息”。

  4. 在详细信息选项卡上,单击“复制到文件”并使用默认值保存文件。

  5. 找到并双击刚刚保存的证书文件。

  6. 在证书常规选项卡上,单击“安装证书”。

  7. 在安装证书向导中,选择“将所有证书放入以下存储”。

  8. 单击“浏览”并选择“受信任的根证书颁发机构”,然后继续执行其余步骤以安装证书。

来源:https ://www.attachmate.com/documentation/gateway-1-1/gateway-admin-guide/data/fxg_add_untrusted_cert.htm


推荐阅读