我花了很多时间阅读各种表格。似乎没有什么工作。我有一个简单的启用Ajax的WCF服务。我可以调用Read方法,它可以找到,发布就是问题。
这是我的web.config
:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="CompanyNotificationService.NotificationManager.MessagesAspNetAjaxBehavior">
<webHttp defaultOutgoingResponseFormat="Json"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="CompanyNotificationService.NotificationManager.Messages">
<endpoint address="" behaviorConfiguration="CompanyNotificationService.NotificationManager.MessagesAspNetAjaxBehavior"
binding="webHttpBinding" contract="CompanyNotificationService.NotificationManager.Messages" bindingConfiguration="defaultRestJson" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="defaultRestJson" crossDomainScriptAccessEnabled="false">
<readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="64" maxNameTableCharCount="2147483647" />
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
我的服务
[ServiceContract(Namespace = "NotificationManager.Messages")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, IncludeExceptionDetailInFaults = true)]
public class Messages
{
[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
public List<Message> Read()
{
Notifications notifications = Notifications.Get();
return notifications.Messages;
}
[OperationContract, WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public void Create(Message models)
{
var i = 1; //I break here, just to inspect everything...not finished
}
[DataContract]
public class Message : IMessage
{
[DataMember]
public string ID { get; set; }
[DataMember]
public string Text { get; set; }
[DataMember]
public DateTime StartDate { get; set; }
[DataMember]
public DateTime ResumeDate { get; set; }
[DataMember]
public string CreatedUser { get; set; }
}
小提琴手信息
POST http://localhost:64394/NotificationManager/Messages.svc/Create HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Referer: http://localhost:64394/NotificationManager/Manage.aspx
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: localhost:64394
Content-Length: 1130
DNT: 1
Connection: Keep-Alive
Pragma: no-cache
Cookie: __AntiXsrfToken=82ba422caa20485690cc905fe6a4b022
HTTP/1.1 400 Bad Request
最后是异常
哦,我几乎从fiddler获得了json服务器在处理请求时遇到错误 异常消息是'格式化程序在尝试反序列化消息时引发异常:反序列化操作'Create'的请求消息正文时出错。遇到意外的角色'm'。'。请参阅服务器日志以获取更多详
异常堆栈跟踪是:
在System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(消息消息,Object []参数).....
models=[{"ID":"1","Text":"here is my message","StartDate":"2013-12-07T06:54:34.396Z","ResumeDate":"2013-12-07T06:54:34.396Z","CreatedUser":"charbaugh"}]
任何帮助都会很棒!我从未在这里发帖,我总能快速找到答案,但这让我失败了!
答案 0 :(得分:2)
当然,异常是因为WCF服务期望一个以{
开头的JSON对象,但您的正文始于m
models=...
。这是不正确的。
对于您的请求,正确的JSON要发送到服务是:
{"models": {"ID":"1","Text":"here is my message","StartDate":"/Date(1320825600000-0800)/","ResumeDate":"/Date(1320825600000-0800)/","CreatedUser":"charbaugh"}}
另请注意,我更改了日期时间内容/格式。有关详细信息,请参阅:How do I format a Microsoft JSON date?
答案 1 :(得分:0)
问题可能出在客户端。我看到你使用ajax来调用你的服务。我看到将对象传递给ajax请求时可能会出现问题,尝试将其作为字符串传递。
data: '[{"ID":"1","Text":"here is my message","StartDate":"2013-12-07T06:54:34.396Z","ResumeDate":"2013-12-07T06:54:34.396Z","CreatedUser":"charbaugh"}]'
此处有更多信息:http://encosia.com/asmx-scriptservice-mistake-invalid-json-primitive/