【问题标题】:Cognos Mashup Service CORS errorCognos Mashup 服务 CORS 错误
【发布时间】:2018-09-15 16:00:42
【问题描述】:

所以我试图让 Cognos Mashup Service 从这里给出的示例中工作:https://www.ibm.com/communities/analytics/cognos-analytics-blog/how-to-use-ibm-10-2-2-cognos-mashup-service-cms-samples-in-ibm-cognos-analytics-11/

当使用内部浏览器在 Eclipse 中运行时,该应用程序实际上运行良好。 但是,当我将默认的 Eclipse 浏览器从内部更改为外部(如 Firefox 或 Chrome)时,连接时出现 CORS 访问错误到 Cognos 11:

跨域请求被阻止:同源策略不允许读取位于https://placeholder/ibmcognos/bi/v1/disp/rds/pagedReportData/report/i72BCF4B83AFE430F9A7F4721DF093FAB?fmt=HTMLFragment&selection=List1&rowLimit=2 的远程资源。 (原因:缺少 CORS 标头“Access-Control-Allow-Origin”)

据我了解,我需要将以下内容添加到我的服务器配置文件中:

<Location />
order allow,deny
allow from all
Header set Access-Control-Allow-Origin "*"
</Location> 

但是,这只会导致 404 错误的无限循环。 那么我做错了什么,为什么内部 eclipse 浏览器可以工作?这是我的简单网络应用程序 html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Generic Authentication Sample</title>
<script>

var myNameSpace=null;
var myUserName=null;
var myPassword=null;

try {
  var objXHR = new XMLHttpRequest();
  } catch (e) {
    try {
        alert('Msxml2.XMLHTTP');
      var objXHR = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
      try {
          alert('Microsoft.XMLHTTP')
        var objXHR = new ActiveXObject('Microsoft.XMLHTTP');
      } catch (e) {
        alert('XMLHttpRequest not supported'); }
      }
  }      

 function doReport()
 {

    myNameSpace=document.getElementById("nameSpace").value;
    myUserName=document.getElementById("userName").value;
    myPassword=document.getElementById("password").value;
    var myReportID=document.getElementById("storeID").value;

    var myFrag = document.getElementById("fragment");

    if(myNameSpace==""||myUserName==""||myPassword=="")
    {
        myFrag.innerHTML="";
        myFrag.innerHTML= "<HTML><BODY><b>Please enter Authentication information ....</b></BODY></HTML>";
        return;
    }

    if(myReportID=="")
    {
        myFrag.innerHTML="";
        myFrag.innerHTML= "<HTML><BODY><b>Please enter the Report ID ....</b></BODY></HTML>";
        return;
    }

    var url = parent.settings.document.getElementById("serverURL").value + "/bi/v1/disp/rds/pagedReportData/report/" + document.getElementById("storeID").value;
    window.status = "Loading Report. Please wait....";
    var frag = document.getElementById("fragment");
    frag.innerHTML= "<HTML><BODY>Loading Report. Please wait....</BODY></HTML>";
    try
    {
        objXHR.open("GET", url, true);
        objXHR.onreadystatechange = callBack;
        objXHR.send(null);
    }
    catch (e)
    {
        alert("Error occurs when doing report.\r\n"+ e);
    }

 }

function doLogon()
{

    var xmlData =
      "xmlData=<auth:credentials xmlns:auth='http://developer.cognos.com/schemas/ccs/auth/types/1'>"
      +"<auth:credentialElements><auth:name>CAMNamespace</auth:name>"
      +"<auth:value><auth:actualValue>"+myNameSpace+"</auth:actualValue></auth:value></auth:credentialElements>"
      +"<auth:credentialElements><auth:name>CAMUsername</auth:name>"
      +"<auth:value><auth:actualValue>"+myUserName+"</auth:actualValue></auth:value></auth:credentialElements>"
      +"<auth:credentialElements><auth:name>CAMPassword</auth:name>"
      +"<auth:value><auth:actualValue>"+myPassword+"</auth:actualValue></auth:value></auth:credentialElements>"
      +"</auth:credentials>";

    try
    {

        var url = parent.settings.document.getElementById("serverURL").value + "/bi/v1/disp/rds/auth/logon";
        objXHR.open("POST", url, false);
        objXHR.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        objXHR.setRequestHeader("Content-length",xmlData.length);
        objXHR.setRequestHeader("Connection","close");
        objXHR.send(xmlData);

        if(objXHR.status == 200)
        {

            // Processes the response from the CMS Authentication service.
            // If we recieve an "accountInfo" element back, we have successfully logged in.
            var accountInfo = objXHR.responseXML.getElementsByTagName("auth:accountInfo");

            if (accountInfo.length > 0)
            {   
                objXHR = new XMLHttpRequest(); //each request can only be opened once
                doReport();
            }
            else
            {
                //Otherwise, prompt error message
                alert("Error occurs when doing logon. Please check input credentials.");
                return;
            }
        }
        else
        {
            var url =  parent.settings.document.getElementById("serverURL").value + "/bi/v1/disp/rds/auth/logon";   
            alert(url);
            showError(objXHR.responseText);
            window.status = "Done";
        }
    }
    catch (e)
    {
        alert("Error occurs when doing logon.\r\n"+e);
    }

}

function showError(msg)
{
    var frag = document.getElementById("fragment");
    frag.innerHTML= "Complete";
    frag.style.visibility = "hidden";
    frag = document.getElementById("error")
    frag.innerHTML= msg;
    frag.style.visibility = "visible";
}

function doLogoff()
{
    try
    {
        objXHR.open("POST", parent.settings.document.getElementById("serverURL").value + "/bi/v1/disp/rds/auth/logoff", false);
        objXHR.send(null);
        checkLogoffStatus();
    }
    catch (e)
    {
        alert("Error occurs when doing logoff.\r\n"+e);
    }
}

function checkLogoffStatus()
{
    if(objXHR.readyState == 4 && objXHR.status == 200)
    {
        window.status = "Successfully log off IBM Cognos Service.";
        alert("Successfully log off IBM Cognos Service.");
    }
    else
    {
        var frag = document.getElementById("fragment");
        frag.innerHTML = objXHR.responseText;
        alert("HTTP ERROR " + objXHR.status + ": " + objXHR.statusText);
        window.status = "Done";
    }
}   

function callBack()
{
    if (objXHR.readyState == 4)
    {

        if (objXHR.status == 200)
        {
            var frag = document.getElementById("fragment");
            frag.innerHTML = objXHR.responseText;
            window.status = "Done";

        }
        else if (objXHR.status == 403)
        {
            doLogon();
        }
        else
        {
            alert("CALLBACK HTTP ERROR " + objXHR.status);
            window.status = "Done";
        }
    }
}


</script>
</head>
<body style="font-family: Verdana, Arial, Sans-Serif; font-weight: normal"><br />
<table align="center" width="95%"
    style="border-bottom-style: solid; border-top-style: solid; border-left-color: #004080; border-top-color: #004080; border-right-color: #004080; border-left-style: solid; border-right-style: solid; border-bottom-color: #004080; border-bottom-width: thin; border-right-width: thin; border-left-width: thin; border-top-width: thin">
    <thead>
        <tr>
            <td
                style="text-align: center; color: #FFFFFF; font-weight: bold; background-color: #0080C0">CMS
            Authentication Sample</td>
        </tr>
    </thead>
    <tr>
      <td>
        <table width="100%">
          <td align="left" width="60%">
            Name Space:&nbsp;&nbsp;<input type="text" size="40", id="nameSpace" value="PLACEHOLDER"><br><br>
            &nbsp;&nbsp;User Name:&nbsp;&nbsp;<input type="text" size="40", id="userName" value="PLACEHOLDER"><br><br>
            &nbsp;&nbsp;&nbsp;&nbsp;Password:&nbsp;&nbsp;<input type="password" size="40", id="password" value="PLACEHOLDER"><br><br>
            &nbsp;&nbsp;&nbsp;&nbsp;Report ID:&nbsp;&nbsp;<input size="40" id="storeID" value="PLACEHOLDER"><br><br>
          </td>
          <td align="left" width="40%">
            <input type="submit" value="Report Output" onClick="doReport()"><br><br>
            <input type="submit" id="logoff" value="Logoff from IBM Cognos Server" onclick="doLogoff()"><br>
          </td>
        </table>
      </td>
    </tr>
    <tr>
      <td
        style="border-top-style: dashed; border-top-color: #000000; border-top-width: medium; border-bottom-style: dashed; border-bottom-width: medium; border-bottom-color: #000000"
            height="100%">
        <DIV id="fragment" style="height: 100%; width: 100%">Your report will appear here</DIV>
      </td>
    </tr>
</table>
</body>
</html>

【问题讨论】:

  • 不确定无限循环,但您的doReport 函数中可能缺少objXHR.withCredentials = true;
  • @tdcc 那到底应该去哪里?
  • objXHR.send(null); 之前。此外,在调试时,无限循环在哪里/何时发生?
  • 如果您添加objXHR.withCredentials = true;,那么您还必须将以下内容添加到您的httpd.conf 文件中:Header set Access-Control-Allow-Credentials "true" 除了添加Access-Control-Allow-Origin 标头
  • 您能否提供完整的请求和响应标头列表 - 这肯定会有所帮助...

标签: web web-applications cors xmlhttprequest cognos


【解决方案1】:

原因浏览器和 Web 服务器的配置不允许 CORS 请求。

此问题是由浏览器和 Web 服务器的现代设计引起的,与 IBM Cognos 产品无关。

这可以通过将请求的资源移动到同一个域或通过启用 CORS = Web 服务器中的跨域资源共享来解决,并且浏览器 IE / IIS 已在浏览器 / 网络服务器链中启用此功能,如果这在那里工作。

有关服务器和客户端的更多信息,请参阅:http://enable-cors.org

对于浏览器,请查看浏览器支持站点,因为这很可能是内部设置。

关于 CORS 的一般信息:

http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

参考:http://www-01.ibm.com/support/docview.wss?uid=swg21698830

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 2018-03-19
    • 1970-01-01
    • 2019-07-10
    • 2018-09-02
    • 2015-09-25
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多