我正在尝试做一些我认为很简单的事情.我需要创建一个WCF服务,我可以通过
JQuery发布.我在我的WCF服务中有一个操作,定义如下:
[OperationContract] [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat=Webmessageformat.Json,ResponseFormat=Webmessageformat.Json)] public string SendMessage(string message,int urgency) { try { // Do stuff return "1"; // 1 represents success } catch (Exception) { return "0"; } }
然后我尝试通过JQuery从ASP.NET页面访问此操作.我访问此操作的JQuery代码如下所示:
function sendMessage(message) { $.ajax({ url: "/resources/services/myService.svc/SendMessage",type: "POST",contentType: "application/json; charset=utf-8",data: ({ message: message,urgency: '1' }),dataType: "json",success: function (data) { alert("here!"); },error: function (req,msg,obj) { alert("error: " + req.responseText); } }); }
当我执行此脚本时,错误处理程序将被触发.在其中,我收到一条错误消息:
“遇到意外的角色’c’.”
此消息包含在长堆栈跟踪中.我的问题是,我做错了什么?我收到了其他帖子,例如这个(How to post an array of complex objects with JSON,jQuery to ASP.NET MVC Controller?)没有任何运气.如何使这种基本交互工作?
谢谢!