wcf服务做的API提供给winform、WPF、手机端、web用,遇到各种各样的问题,终于解决了,注意以下加粗的代码
服务接口:
[WebGet(UriTemplate = "all/{empName}", ResponseFormat = WebMessageFormat.Json)]
IEnumerable<Employee> GetAll(string empName);
[WebInvoke(Method = "*",
UriTemplate = "/",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare
//BodyStyle = WebMessageBodyStyle.WrappedRequest
)]
IEnumerable<Employee> Insert(Employee employee);
接口实现类:
public IEnumerable<Employee> GetAll(string empName)
{
if (empName == null)
{
return Employees;
}
else
{
var query = from m in Employees
where m != null && m.Name == empName
select m;
return query.ToList();
}
}
public IEnumerable<Employee> Insert(Employee employee)
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");
if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
{
return null;
}
Employees.Add(employee);
return Employees;
}
完整配置文件:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<!--这里必须设置-->
<!--<webHttp />-->
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="RESTEndpoint" contentTypeMapper="WcfServices.Service.Interface.RESTContentTypeMapper,WcfServices.Service.Interface"/>
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingJSONP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="WcfServices.Service.EmployeesService">
<endpoint kind="webHttpEndpoint"
binding="webHttpBinding"
bindingConfiguration="webHttpBindingJSONP"
behaviorConfiguration="webBehavior"
endpointConfiguration="RESTEndpoint"
address="http://127.0.0.1:3721/employees"
contract="WcfServices.Service.Interface.IEmployees">
</endpoint>
</service>
</services>
</system.serviceModel>
web调用:
$.ajax({
type: "Get",
url: "http://127.0.0.1:3721/employees/all/张三",
dataType: "jsonp",
success: function (employees) {
console.log("success",employees);
},
error:function(result_data){
console.log("error",result_data);
}
});
var data = {"Id":005,"Name":"陈九","Department":"秘书组","Grade":"G10"};
var data_string = JSON.stringify(data);
$.ajax({
type: "POST",
url: "http://127.0.0.1:3721/employees/",
data:JSON.stringify(data),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (employees) {
console.log("success",employees);
},
error:function(result_data){
console.log("error",result_data);
}
});
参考资料
- JQuery.Ajax + 跨域 (crossDomain) + POST + JSON + WCF RESTful, 5大陷阱和解决方案 - Ivan Zou
http://www.tuicool.com/articles/z67Bvi
Problem sending JSON data from JQuery to WCF REST method
https://stackoverflow.com/questions/4875195/problem-sending-json-data-from-jquery-to-wcf-rest-method