Wcf跨域
1.代码编写
[WebInvoke(Method="GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat=WebMessageFormat.Json,RequestFormat= WebMessageFormat.Json)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]
publicclass OneService : IOneService
2.配置文件
app.config内容如下
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<!--部署服务库项目时,必须将配置文件的内容添加到
主机的app.config文件中。
System.Configuration不支持库的配置文件。
-->
<system.serviceModel>
<services>
<service behaviorConfiguration="WcfService1.OneService"name="WcfService1.OneService">
<endpoint address="norml"binding="basicHttpBinding"contract="WcfService1.IOneService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="Jweb"binding="webHttpBinding"behaviorConfiguration="webhttpBehavior"b indingConfiguration="webBinding"contract="WcfService1.IOneService"/>
<endpoint address="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://192.168.1.106:9001"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webJSBehavior">
<enableWebScript />
</behavior>
<behavior name="webhttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WcfService1.OneService">
<!--为避免泄漏元数据信息,
请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="True"/>
<!--要接收故障异常详细信息以进行调试,
请将以下值设置为 true。
在部署前
设置为 false 可避免泄漏异常信息-->
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<!--crossDomainScriptAccessEnabled指定脚本可以跨域访问-->
<binding name="webBinding"crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"aspNetCompatibilityEnabled= "true">
<baseAddressPrefixFilters>
<add prefix="string"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0"sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
class Program
{
staticvoid Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(OneService)))
{
host.Opened +=delegate
{
Console.WriteLine("CalculaorService已经启动,按任意键终止服务!"); };
host.Open();
Console.ReadKey();
}
}
}
3.Jquery调用
$.ajax({
url: 'http://192.168.1.106:9001/jweb/GetData?jsoncallback=?' type: 'GET',
dataType: "json",
data: { value: 10 },
success: function (data) {
alert(data);
}
});
$.ajax({
url: 'http://192.168.1.106:9001/jweb/AddOrder?jsoncallback=?' type: 'POST',
dataType: "json",
data: { orderID: 10 },
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data.AddOrderResult);
}
});
4.搭载服务
第三方应用程序搭载,不能用iis搭载服务。