views:

624

answers:

1

I have a WCF Service Library exposed through my ASPX site as follows

[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebInvoke(
Method= "POST",
RequestFormat=System.ServiceModel.Web. WebMessageFormat .Json,
ResponseFormat=System.ServiceModel.Web.WebMessageFormat .Json)]
LogonResponse Logon(LogonRequest logonRequest);


[System.Runtime.Serialization.DataContract]
[ Serializable()]
public class LogonRequest
{
[System.Runtime.Serialization.DataMember]
public string EMailAddress;
[System.Runtime.Serialization.DataMember]
public string Password;
}

In My test page I can call either through MS Ajax : -

<asp:ScriptManager ID ="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/testService.svc" />
</Services>
</asp:ScriptManager>
.
.
.
function clsLogonRequest(eMailAddress, password) {
this .EMailAddress = eMailAddress;
this .Password = password;
}

function login(eMailAddress, password) {
var LogonRequest = new clsLogonRequest(eMailAddress, password);
revx.API.IAPI.Logon(LogonRequest, onSuccess, onFailure);
}

function onSuccess(result) {
$( "#txtSessionId").val(result.SessionId);
$( "#txtUserName").val(result.Name);
$( "#txtUserId").val(result.UserId);
}

which works fine, or through a jQuery $.ajax call: -

$(document).ready(function() {
$( "#Button1").click(function() {
var LogonRequest = new clsLogonRequest( '*************' , '***********');
$.ajax({
type: "POST",
url: "testService.svc/Logon",
data: "{'logonRequest':" + JSON.stringify(LogonRequest) + "}" ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
}
});
});
});

Which does not - under FireBug I can see the 500 Internal Server Error message and the exception starts

{"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message":"The token '\"' was expected but found '''.","StackTrace":"   at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)\u000d\u000a   at System.Xml.XmlExceptionHelper .ThrowTokenExpected(XmlDictionaryReader reader, String expected, Char found)\u000d\u000a   at System.Runtime...

Why does it appear that the jQuery call is passing XML when I'm specifically telling it not to (and how can I stop it so that calls from all sources are handled as naturally as possible)?

Thanks in advance.

EDIT:

Thanks for the suggestions, I've looked at what you said and think that my explanation of the problem was not clear enough.

The problem is not that JSON is not being sent, it is being sent and is in the correct format, I can see that from firebug. The problem is that the WCF Service Library appears to be expecting XML and falls over when it receives JSON.

Just to be sure I haven't overlooked the suggested solution, here is the web.Config extract - I've tried removing from the behaviour and removing the behaviorConfiguration attribute from the services/service/endpoint tag and that just makes the whole thing fail without getting out of the page.

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name ="MyServiceTypeBehaviors ">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name ="AspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled= "true " />
<services>
<service name ="test.API.API" behaviorConfiguration = " MyServiceTypeBehaviors">
<endpoint behaviorConfiguration="AspNetAjaxBehavior" binding = " webHttpBinding" contract="test.API.IAPI" />
<endpoint contract ="IMetadataExchange" binding= " mexHttpBinding" address="mex" />
</service>
</services>
</system.serviceModel>

Thanks In Advance

+3  A: 
Drew Marsh
thanks for the suggestion, I have edited my original post to clarify.
bean
The exception will always look like it's parsing XML because that's the model used by WCF even when the data is not truly XML. In the case of JSON the DataContractJsonSerialzer is being used, but even that inherits from XmlObjectSerializer and so the stack can make it look like it's working with XML. If you could post the full stack trace that would be useful as well. Everything else about your config looks fine, but with enableWebScript on there you MUST pass that wrapper object like I said. If you remove the enableWebScript you will actually have to set message encoding to JSON yourself.
Drew Marsh