WCF - "Unexpected character" c ".

I am trying to do something that, in my opinion, will be simple. I need to create a WCF service that I can send through jQuery. I have an operation in my WCF service that is defined as follows:

[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"; } } 

Then I try to access this operation from an ASP.NET page through jQuery. My jQuery code to access this operation is as follows:

 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); } }); } 

When I execute this script, the error handler is disabled. An error message appears in it:

"An unexpected character 'c' was detected."

This message is included in a long stack trace. My question is: what am I doing wrong? I got other messages like this one ( How to send an array of complex objects with JSON, jQuery to ASP.NET MVC Controller? ) With no luck. How do I get this basic interaction?

Thanks!

+6
jquery wcf
source share
1 answer

I think you need to back up your json data in the request. More details here . You might also want to parse the incoming response data, as it will be in a string sequence. A common library suitable for the task can be found here .

For example: data: '{message: "message", urgency: "1"}',

+3
source share

All Articles