在复杂的服务器情况下由于CORS而访问XMLHttpRequest

时间:2019-07-22 20:56:55

标签: javascript cors thingworx ptc-windchill

我有两台服务器,我都控制。一个是thingworx服务器,该服务器与另一台thingworx服务器对话,即windchill服务器,并显示我的网页。我有一个与windchill服务器对话的somethingworx mashup。它从Windchill服务器中提取图像和pdf,并允许混搭程序的操作者更改图像或pdf,然后将其放回服务器。我解决了从服务器提取图像时出现的CORS问题,但是现在我在发布到服务器时遇到了CORS错误。

我尝试将CORS过滤器放在Thingworx服务器上,但没有任何乐趣。我不得不告诉脚本将图像作为跨域拉出,所以我在想也有一些适当的方法来请求跨域POST。

$.ajax({
    method: 'POST',
    url: urlString,
    enctype: 'multipart/form-data',
    processData: false,
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST', 
    data: fd
    //crossOrigin: true
}).done(function(data) {
    console.log('success', data) 
});

在chrome中关闭CORS检查非常有用。

1 个答案:

答案 0 :(得分:0)

对于当前版本的Windchill,您可以根据以下PTC文章配置CORS:https://support.ptc.com/help/wnc/r12.0.0.0/en/#page/Windchill_Help_Center%2FFileVaultConfigWCforCORS.html%23

这需要在Windchill的Tomcat配置中设置CORS过滤器。

您似乎正在尝试将内容从Windchill服务器移入或移出,因此我认为以上文章适用于此用例。

步骤摘要:

使用以下过程将CORS过滤器配置为允许跨域HTTP请求。 此配置适用于Windchill主站点和文件服务器站点。

  1. 导航到 \ codebase \ WEB-INF \ web.xml。
  2. 使用以下ContentCorsFilter和ContentHttpHeaderSecurityFilter以及映射配置更新web.xml文件:
<filter>
    <filter-name>ContentCorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>[ALLOWED_ORIGINS]</param-value>
    </init-param>
    <init-param>
        <param-name>cors.support.credentials</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,OPTIONS</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Content-Type,X-Requested-With</param-value>
    </init-param>
</filter>
<filter>
    <filter-name>ContentHttpHeaderSecurityFilter</filter-name>
    <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
    <init-param>
        <param-name>antiClickJackingOption</param-name>
        <param-value>ALLOW-FROM</param-value>
    </init-param>
    <init-param>
        <param-name>antiClickJackingUri</param-name>
        <param-value>[ALLOWED_ORIGINS]</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>ContentCorsFilter</filter-name>
    <url-pattern>/servlet/WindchillAuthGW/wt.content.ContentHttp/viewContent/*</url-pattern>
    <url-pattern>/servlet/WindchillAuthGW/wt.fv.master.StandardMasterService/doDirectDownload/*</url-pattern>
    <url-pattern>/servlet/WindchillAuthGW/wt.fv.replica.StandardReplicaService/doDownload/*</url-pattern>
    <url-pattern>/servlet/WindchillAuthGW/wt.fv.replica.StandardReplicaService/doIndirectDownload/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>ContentHttpHeaderSecurityFilter</filter-name>
    <url-pattern>/servlet/WindchillAuthGW/wt.content.ContentHttp/viewContent/*</url-pattern>
    <url-pattern>/servlet/WindchillAuthGW/wt.fv.master.StandardMasterService/doDirectDownload/*</url-pattern>
    <url-pattern>/servlet/WindchillAuthGW/wt.fv.replica.StandardReplicaService/doDownload/*</url-pattern>
    <url-pattern>/servlet/WindchillAuthGW/wt.fv.replica.StandardReplicaService/doIndirectDownload/*</url-pattern>
</filter-mapping>

使用逗号分隔的列表用所需的网址更新cors.allowed.origin和antiClickJackingUri参数。不要使用星号(*),因为cors.support.credentials必须为true。

  1. 保存web.xml文件。
  2. 重新启动Windchill服务器。

请始终使用最新的PTC技术支持和帮助文章获取最新的最新信息,因为这些步骤可能会更改。

相关问题