1、能被ajax get
2、能post
3、wcf正常调用
实现:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[JavascriptCallbackBehavior(UrlParameterName = "jsoncallback")]
public class WCFJsonTest : IWCFJsonTest
{ public List<TestModel> GetTest(string id)
{
List<TestModel> list = new List<TestModel>();
TestModel t = new TestModel();
t.Ids = ;
t.Names = id;
list.Add(t); TestModel t1 = new TestModel();
t1.Ids = ;
t1.Names = id+"_"+Guid.NewGuid().ToString();
list.Add(t1); return list;
} public TestModel PostTest(string id, string name)
{
TestModel t1 = new TestModel();
t1.Ids = int.Parse(id);
t1.Names = name + "_" + Guid.NewGuid().ToString();
return t1;
} public TestModel PostTest1(TestModel model)
{
TestModel t1 = new TestModel();
t1.Ids = model.Ids;
t1.Names = model.Names + "_" + Guid.NewGuid().ToString();
return t1;
}
}
接口:
[ServiceContract] public interface IWCFJsonTest
{
[OperationContract]
[WebGet(UriTemplate = "/GetTest/{id}", ResponseFormat = WebMessageFormat.Json)]
List<TestModel> GetTest(string id); [OperationContract]
[WebInvoke(Method="GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
TestModel PostTest(string id, string name); [OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
TestModel PostTest1(TestModel model);
}
配置:
endpoint配置两个一个web使用webHttpBinding,一个给wcf
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
<services>
<service name="BLL.WCFJsonTest" behaviorConfiguration="AllBehavior" >
<endpoint contract="IBLL.IWCFJsonTest" binding="wsHttpBinding" bindingConfiguration="WsHttpBinding_TEST" address="wcf" />
<endpoint kind="webHttpEndpoint" contract="IBLL.IWCFJsonTest" binding="webHttpBinding" bindingConfiguration="basicTransport" behaviorConfiguration="web" address="" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services> <behaviors>
<serviceBehaviors>
<behavior name="AllBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="basicTransport" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
<wsHttpBinding>
<binding name="WsHttpBinding_TEST" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="" maxReceivedMessageSize=""
messageEncoding="Text" textEncoding="utf-8"
useDefaultWebProxy="true">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength=""
maxBytesPerRead="" maxNameTableCharCount="" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint crossDomainScriptAccessEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
调用:
wcf正常调用地址:http://xxxxxx:xxxx/JsonTestService.svc
post:http://xxxxxx:xxxx/JsonTestService.svc/PostTest
get:http://xxxxxx:xxxx/JsonTestService.svc/GetTest/2
例如:
string srcString=GetData("", "http://xxxxxx:xxxx/JsonTestService.svc/GetTest/2"); srcString = PostHelper.GetPermissionRemoteData("http://xxxxxx:xxxx/JsonTestService.svc/PostTest","{\"id\":\"10\",\"name\":\"张三\"}","text/json"); string jsonStr = "{\"Ids\":\"10\",\"Names\":\"张三1\"}";
srcString = PostHelper.GetPermissionRemoteData("http://xxxxxx:xxxx/JsonTestService.svc/PostTest1", "{\"model\": "+ jsonStr+" }", "text/json"); WCFJsonTestClient client = new WCFJsonTestClient();
var r = client.GetTest("");
var r1 = client.PostTest("", "a");
var r2 = client.PostTest1(new TestModel() { Ids = , Names = "" });
$.ajax({
type: "GET",
dataType: "jsonp",
url: 'http://xxxxxx:xxxx/JsonTestService.svc/GetTest/2',
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log('err1' + XMLHttpRequest);
console.log('err2' + textStatus);
console.log('err3' + errorThrown);
}
});