gpt4 book ai didi

Javascript 跨域 WCF 自托管限制

转载 作者:行者123 更新时间:2023-11-28 09:34:32 25 4
gpt4 key购买 nike

我有一个有点独特的问题。我有一个静态 HTML 和 Javascript 页面,需要从 Javascript 调用 Web 服务。此 WCF Web 服务将作为 Windows 服务自行托管在单独的服务器上。由于跨域限制,我在尝试在 javascript 页面和 Web 服务之间进行通信时遇到了问题。我已经看到了一些关于如何解决此问题的示例,但由于我的网页将是静态的并且不是由 IIS 提供服务,所以我不确定如何解决它。

我在下面附上了一些代码。任何帮助,将不胜感激。

应用程序配置

<?xml version="1.0"?>
<configuration>

<configSections>
</configSections>
<connectionStrings>
<add name="test.XKORE.MobileDeviceServices.Properties.Settings.ConnectionString"
connectionString="Data Source=tester;Initial Catalog=test;User ID=testc;Password=testp" />
</connectionStrings>
<system.web>
<compilation debug="true"/>
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="test.XKORE.MobileDeviceServices.XKOREMobileService" behaviorConfiguration="XKOREMobileServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8523/test/XKORE/XKOREMobileService" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="test.XKORE.MobileDeviceServices.IXKOREMobileService" bindingNamespace="http://test.XKORE.MobileDeviceServices" />
<endpoint address="mex" binding="mexHttpBinding" contract="test.XKORE.MobileDeviceServices.IXKOREMobileService" bindingNamespace="http://test.XKORE.MobileDeviceServices" />

</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="XKOREMobileServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

界面

[ServiceContract]
public interface IXKOREMobileService
{
[OperationContract]
string GetChartData();

// TODO: Add your service operations here
}

SOAP 请求

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IXKOREMobileService/GetChartData</Action>
</s:Header>
<s:Body>
<GetChartData xmlns="http://tempuri.org/" />
</s:Body>
</s:Envelope>

Javascript(不工作)

var response = BuildSOAPMessage('GetChartData');
alert(response);

function BuildSOAPMessage (func) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "http://localhost:8523/test/XKORE/XKOREMobileService", false);

var msg = '';
msg += '<?xml version="1.0" encoding="utf-8"?>'
msg += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">'
msg += '<soapenv:Header/>'
msg += '<soapenv:Body>'
msg += '<tem:' + func + '/>'
msg += '</soapenv:Body>'
msg += '</soapenv:Envelope>'
alert (msg);

// Send the POST request
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.setRequestHeader("SOAPAction", "http://tempuri.org/IXKOREMobileService/GetJSONChartData");
xmlhttp.send(msg);

return xmlhttp.responseXML;
}

最佳答案

您的问题并不是唯一的...这是对 WS 的调用由于 CORS 而失败。

因此,您必须通过实现 IDispatchMessageInspector 在 WS 中设置 HTTP header “Access-Control-Allow-Origin” :

public void BeforeSendReply(ref Message reply, object correlationState)
{
var httpResponse = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
if (httpResponse != null)
{
//CORS
httpResponse.Headers["Access-Control-Allow-Origin"] = "*";
}
}

关于Javascript 跨域 WCF 自托管限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13279502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com