使用jQuery使用WCF服务

到目前为止,我已经使用了Web服务,它运行良好。 我添加了一个新的WCF服务。
我使用jQuery调用服务。 这就是我使用jQuery来使用Web服务的方法:

$.ajax({ dataType: 'json', processData: false, type: 'POST', contentType: "application/json", url: url, context: s.context, data: JSON.stringify(s.data), error: function (xhr, textStatus, errorThrown) { if (s.error != null) { s.error(xhr, textStatus, errorThrown); } if (s.error_addition != null) { s.error_addition(xhr, textStatus, errorThrown); } }, success: function (data, textStatus, xhr) { s.success(data.d, textStatus, xhr); subscriber.setDone(data.d); } }); 

我不能使用此方法来使用WCF服务。 这是WCF服务定义的方式:

 [ServiceContract] public interface ISystemService { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] Reason[] GetCallReasons(); } [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class SystemService : SimTaskService, ISystemService { public Reason[] GetCallReasons() { //... } } 

我没有为WCF服务的Web配置添加任何内容。

使用fiddler,这是响应标头:

 HTTP/1.1 415 Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'text/xml; charset=utf-8'. 

这是我的web.config:

      

有谁知道如何解决它?

我使用过这个教程,效果很好。
因为我使用asp.net和wcf,我需要添加:

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

作为类服务的属性。
另外将此行添加到web.config:

  

希望这可以帮助!

尝试将此添加到您的WCF配置中:

                              

此外,这是我在我们的应用程序中工作的:

 $.ajax({ url: systemServiceURL, data: JSON.stringify(data), type: "POST", processData: true, contentType: "application/json", timeout: 10000, dataType: "json", beforeSend: function(req) { req.setRequestHeader('Authorization','Basic '); }, success: function(msg) { //do stuff here }, error: function(msg) { alert('error : ' + msg.d); } }); 

编辑:尝试修改编辑到问题中的web.config

      

您需要指定restful服务将使用Json对象

此链接将有助于http://www.codeproject.com/KB/ajax/jQueryWCFRest.aspx

更改配置: