积极答复者
jquery以post方式调用wcf出现错误

问题
-
wcf和jquery使用get方式没有问题,但是使用post方式就报错:"对操作“Delete”的请求消息正文进行反序列化时出现错误。遇到意外字符“i”。"
[OperationContract]
[WebInvoke(BodyStyle=WebMessageBodyStyle.WrappedRequest,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
public string Delete(string id)
{
var db = new worldlinkEntities();
try
{
db.DelBilling(id);
return true;
}
catch
{
return false
}
}
客户端:$.ajax({
url:s,
type:"POST",
dataType:"json",
contentType:"application/json",
data:{"id":"1084117"},
error:function(data){alert(data);},
success:function(){alert("df")}
})
答案
-
参考一下这个文章:
http://geekswithblogs.net/Nettuce/archive/2009/10/18/wcf-jquery-rest-json-service.aspx
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFrameworkMoniker=".NETFramework,Version=v4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<serviceActivations>
<add relativeAddress="Service" service="Service" />
</serviceActivations>
</serviceHostingEnvironment>
<services>
<service name="Service">
<endpoint behaviorConfiguration="webHttpBehaviour" binding="webHttpBinding" contract="Service" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>Service
using System.IO;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
[ServiceContract, AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
[OperationContract, WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json)]
public Stream Post(string message, int x, int y)
{
return new MemoryStream(Encoding.UTF8.GetBytes("<b>" + message + (x + y) + "</b>"));
}
}jQuery
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-nightly.min.js"></script>
<script type="text/javascript">
$.ajax({
url: "service/post",
type: "POST",
contentType: "application/json",
data: '{"x":"1","y":"2","message":"The answer is: "}',
dataType: "html",
success: function(data) { $('body').html(data); }
});
</script>
</head>
<body>
</body>
</html>
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
- 已建议为答案 Frank Xu LeiModerator 2011年3月10日 12:51
- 已标记为答案 Mog Liang 2011年3月11日 9:07
全部回复
-
把$.ajax里的contenttype去除后报错""Message":"传入消息的消息格式不应为“Raw”。此操作的消息格式应为 'Xml', 'Json'。这可能是因为绑定尚未配置 WebContentTypeMapper。"
我曾经讲的WebCast里Demo:WCF与Ajax开发实践(6)AjaxJSONWCFWebSite.这个使用的是微软的Ajax Library。
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
-
参考一下这个文章:
http://geekswithblogs.net/Nettuce/archive/2009/10/18/wcf-jquery-rest-json-service.aspx
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFrameworkMoniker=".NETFramework,Version=v4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<serviceActivations>
<add relativeAddress="Service" service="Service" />
</serviceActivations>
</serviceHostingEnvironment>
<services>
<service name="Service">
<endpoint behaviorConfiguration="webHttpBehaviour" binding="webHttpBinding" contract="Service" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>Service
using System.IO;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
[ServiceContract, AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
[OperationContract, WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json)]
public Stream Post(string message, int x, int y)
{
return new MemoryStream(Encoding.UTF8.GetBytes("<b>" + message + (x + y) + "</b>"));
}
}jQuery
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-nightly.min.js"></script>
<script type="text/javascript">
$.ajax({
url: "service/post",
type: "POST",
contentType: "application/json",
data: '{"x":"1","y":"2","message":"The answer is: "}',
dataType: "html",
success: function(data) { $('body').html(data); }
});
</script>
</head>
<body>
</body>
</html>
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
- 已建议为答案 Frank Xu LeiModerator 2011年3月10日 12:51
- 已标记为答案 Mog Liang 2011年3月11日 9:07
-
参考徐大的示例终于成功了,呵呵,我发现这里既有wcf的原因,也有jquery的原因,post和get又有不同,rest和不使用也有不同,问题多数出在BodyStyle的设置上,只能死记相关设置了。
呵呵,搞定就好啊,我也是使用Google查到的。
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
-
微软的东西还是配和自己的微软的东西比较好,会少了很多麻烦。
呵呵,是啊,自己的产品肯定优先支持自己的啊~
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/