我花了很多时间阅读各种表格。似乎什么都不起作用。我有一个简单的启用 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; }
}
和 fiddle 手信息
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
最后是异常(exception)
The server encountered an error processing the request.
The exception message is 'The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Create'. Encountered unexpected character 'm'.'. See server logs for more details.The exception stack trace is:
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters) .....
哦,我差点就从 fiddler 那里得到了 json
models=[{"ID":"1","Text":"here is my message","StartDate":"2013-12-07T06:54:34.396Z","ResumeDate":"2013-12-07T06:54:34.396Z","CreatedUser":"charbaugh"}]
任何帮助都会很棒!我从未在这里发过帖子,我总是能够快速找到答案,但这让我崩溃了!
最佳答案
当然,异常(exception)是因为 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?
关于WCF 方法不接受 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20438552/