我试图从jQuery访问Windows托管的WCF。我在使用POST时无法访问发送到服务器的数据。所以我用'GET'并能够将数据发布到服务器。但客户端没有收到服务器的响应。我得到了Cross-Origin'请求被阻止'错误。
jQuery客户端代码
function callWCF() {
var obj = {};
obj.data = "XLGS";
var inputData = JSON.stringify(obj);
var url = 'http://localhost:8733/TestService/GetData?value=' + 'test';
$.ajax({
type: 'GET',
url: url,
data: {},
//data: { parameters: "{'process':'AutoComplete', 'getall':'false'}" },
contentType: '',
dataType: '',
success: function (httpResponse) {
alert("Success");
},
error: function (httpResponse) {
alert("Error");
}
});}
WCF服务代码
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml)]
public string GetData(string value)
{
return value + " Response from service";
}
WCF主机配置
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="TestServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="TestServiceBehavior" name="WCFAjax.GateWayService">
<endpoint address="" binding="webHttpBinding" contract="WCFAjax.IGateWayService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="web" binding="webHttpBinding" contract="WCFAjax.IGateWayService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/TestService"/>
</baseAddresses>
</host>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<!--<services>
<service name="WCFAjax.GateWayService">
<endpoint address=""
binding="webHttpBinding"
bindingConfiguration=""
name="WCFAjax.GateWayService"
contract="WCFAjax.IGateWayService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/TestService"/>
</baseAddresses>
</host>
</service>
</services>-->
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />-->
何解决?