Compatible with multiple call Methods
1. ajax get
2. post
3. Normal call of wcf
Implementation:
1 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 2 [JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")] 3 public class WCFJsonTest : IWCFJsonTest 4 { 5 6 public List<TestModel> GetTest(string id) 7 { 8 List<TestModel> list = new List<TestModel>(); 9 TestModel t = new TestModel();10 t.Ids = 1;11 t.Names = id;12 list.Add(t);13 14 TestModel t1 = new TestModel();15 t1.Ids = 1;16 t1.Names = id+"_"+Guid.NewGuid().ToString();17 list.Add(t1);18 19 return list;20 }21 22 23 public TestModel PostTest(string id, string name)24 {25 TestModel t1 = new TestModel();26 t1.Ids = int.Parse(id);27 t1.Names = name + "_" + Guid.NewGuid().ToString();28 return t1;29 }30 31 32 public TestModel PostTest1(TestModel model)33 {34 TestModel t1 = new TestModel();35 t1.Ids = model.Ids;36 t1.Names = model.Names + "_" + Guid.NewGuid().ToString();37 return t1;38 }39 }
Interface:
1 [ServiceContract] 2 3 public interface IWCFJsonTest 4 { 5 [OperationContract] 6 [WebGet(UriTemplate = "/GetTest/{id}", ResponseFormat = WebMessageFormat.Json)] 7 List<TestModel> GetTest(string id); 8 9 [OperationContract]10 [WebInvoke(Method="GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]11 TestModel PostTest(string id, string name);12 13 [OperationContract]14 [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]15 TestModel PostTest1(TestModel model);16 }
Configuration:
Endpoint: Configure two web sites to use webHttpBinding, and one
1 <system.serviceModel> 2 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> 3 <services> 4 <service name="BLL.WCFJsonTest" behaviorConfiguration="AllBehavior" > 5 <endpoint contract="IBLL.IWCFJsonTest" binding="wsHttpBinding" bindingConfiguration="WsHttpBinding_TEST" address="wcf" /> 6 <endpoint kind="webHttpEndpoint" contract="IBLL.IWCFJsonTest" binding="webHttpBinding" bindingConfiguration="basicTransport" behaviorConfiguration="web" address="" /> 7 <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 8 </service> 9 </services>10 11 <behaviors>12 <serviceBehaviors>13 <behavior name="AllBehavior">14 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />15 <serviceDebug includeExceptionDetailInFaults="true" />16 </behavior>17 <behavior name="">18 <serviceMetadata httpGetEnabled="true" />19 <serviceDebug includeExceptionDetailInFaults="false" />20 </behavior>21 </serviceBehaviors>22 <endpointBehaviors>23 <behavior name="web">24 <webHttp helpEnabled="true"/>25 </behavior>26 </endpointBehaviors>27 </behaviors>28 <bindings>29 <webHttpBinding>30 <binding name="basicTransport" crossDomainScriptAccessEnabled="true"/>31 </webHttpBinding>32 <wsHttpBinding>33 <binding name="WsHttpBinding_TEST" closeTimeout="00:01:00"34 openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"35 allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"36 maxBufferPoolSize="524288" maxReceivedMessageSize="65536"37 messageEncoding="Text" textEncoding="utf-8"38 useDefaultWebProxy="true">39 <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"40 maxBytesPerRead="4096" maxNameTableCharCount="16384" />41 <security mode="None">42 <transport clientCredentialType="None" proxyCredentialType="None"43 realm="" />44 <message clientCredentialType="UserName" algorithmSuite="Default" />45 </security>46 </binding>47 </wsHttpBinding>48 </bindings>49 <standardEndpoints>50 <webHttpEndpoint>51 <standardEndpoint crossDomainScriptAccessEnabled="true"/>52 </webHttpEndpoint>53 </standardEndpoints>54 </system.serviceModel>
Call:
Normal call address of wcf: http: // xxxxxx: xxxx/JsonTestService. svc
Post: http: // xxxxxx: xxxx/JsonTestService. svc/PostTest
Get: http: // xxxxxx: xxxx/JsonTestService. svc/GetTest/2
For example:
1 string srcString = GetData ("", "http: // xxxxxx: xxxx/JsonTestService. svc/GetTest/2 "); 2 3 srcString = PostHelper. getPermissionRemoteData ("http: // xxxxxx: xxxx/JsonTestService. svc/PostTest "," {\ "id \": \ "10 \", \ "name \": \ "Zhang San \"} "," text/json "); 4 5 string jsonStr = "{\" Ids \ ": \" 10 \ ", \" Names \ ": \" Zhang San 1 \ "}"; 6 srcString = PostHelper. getPermissionRemoteData ("http: // xxxxxx: xxxx/JsonTestService. svc/PostTest1 "," {\ "model \": "+ jsonStr +"} "," text/json "); 7 8 WCFJsonTestClient client = new WCFJsonTestClient (); 9 var r = client. getTest ("1"); 10 var r1 = client. postTest ("1", "a"); 11 var r2 = client. postTest1 (new TestModel () {Ids = 1, Names = "2 "});
1 $.ajax({ 2 type: "GET", 3 dataType: "jsonp", 4 url: 'http://xxxxxx:xxxx/JsonTestService.svc/GetTest/2', 5 success: function (data) { 6 console.log(data); 7 }, 8 error: function (XMLHttpRequest, textStatus, errorThrown) { 9 console.log('err1' + XMLHttpRequest);10 console.log('err2' + textStatus);11 console.log('err3' + errorThrown);12 }13 });