Wcf resut服务
外部地址调用Wcf resut服务,POST方式好像不支持呀,我是实验了多次也没有成功。
这里演示一下GET方式。 客户端调用使用的JsonP
//接口 namespace Jiang.WebServ { [ServiceContract(Name = "IServices", Namespace = "http://www.msdn.com/Serv/Medicine")] public interface IServices { [OperationContract(Name = "GetName")] [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string GetName (string str); } }
//实现
namespace Jiang.WebServ { [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)] public class Services : IServices { #region IServices 成员 public string GetName(string str) { return "Hello :"+str;} } }
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="OracleConnectionString" connectionString="Password=*****;User ID=*****;Data Source=ORACLE41"/> </connectionStrings> <system.serviceModel> <!--添加这个--> <standardEndpoints> <webScriptEndpoint> <standardEndpoint name="" crossDomainScriptAccessEnabled="true"/> </webScriptEndpoint> </standardEndpoints> <!-- --> <services> <service name="Jiang.WebServ.Services" behaviorConfiguration="WebbehaviorConfig"> <endpoint address="" binding="webHttpBinding" contract="Jiang.WebServ.IServices" bindingConfiguration="httpBindingConfig"/> </service> </services> <serviceHostingEnvironment> <serviceActivations> <!-- 设置虚拟.svc文件--> <add service="Jiang.WebServ.Services" relativeAddress="WebServ.svc"/> </serviceActivations> </serviceHostingEnvironment> <bindings> <webHttpBinding> <binding name="httpBindingConfig" crossDomainScriptAccessEnabled="true" openTimeout="00:05:00" receiveTimeout="00:05:00" sendTimeout="00:05:00" maxBufferPoolSize="10240000" maxBufferSize="10240000" maxReceivedMessageSize="10240000"> <security mode="None"/> </binding> </webHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="WebbehaviorConfig"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior> <webHttp helpEnabled="true"/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <directoryBrowse enabled="true"/> </system.webServer> <system.web> <compilation debug="true"/> </system.web> </configuration>
客户端
$.ajax({ type: "GET", dataType: "jsonp", jsonp: "callback", jsonpCallback: "callback", url: "http://192.168.1.14/webSvc.svc/GetName?str=jiang", success: function (data) { if (data.length == 0) { $(control).remove(); } else { alert(data); } } });