jquery – WCF – “遇到了意想不到的字符’c’.”

我正在尝试做一些我认为很简单的事情.我需要创建一个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?)没有任何运气.如何使这种基本交互工作?

谢谢!

解决方法

我认为你必须在请求中字符串化你的json数据.更多信息 here.您可能还需要解析传入的响应数据,因为它将作为回报进行字符串化.适用于该任务的通用库可以在 here找到.

例如:data:'{message:“message”,urgency:“1”}’,

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...