Problem beim Aufrufen der WCF-Dienstbibliothek aus jQuery

Ich habe eine WCF-Dienstbibliothek über meine ASPX-Site wie folgt verfügbar gemacht

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

Auf meiner Testseite kann ich entweder über MS Ajax anrufen: -

<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);
name.API.IAPI.Logon(LogonRequest, onSuccess, onFailure);
}

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

das funktioniert gut, oder durch einen jQuery $ .ajax-Aufruf: -

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

Was nicht der Fall ist - unter FireBug kann ich die 500 Internal Server Error-Meldung sehen und die Ausnahme wird gestartet

{"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...

Warum scheint es, dass der jQuery-Aufruf XML übergibt, wenn ich ausdrücklich darauf hinweise (und wie kann ich ihn stoppen, damit Aufrufe aus allen Quellen so natürlich wie möglich behandelt werden)?

Danke im Voraus.

BEARBEITEN:

Vielen Dank für die Vorschläge. Ich habe mir angesehen, was Sie gesagt haben, und bin der Meinung, dass meine Erklärung des Problems nicht klar genug war.

Das Problem ist nicht, dass JSON nicht gesendet wird, es wird gesendet und es hat das richtige Format. Ich kann das anhand des Firebugs erkennen. Das Problem besteht darin, dass die WCF-Servicebibliothek anscheinend XML erwartet und beim Empfang von JSON abstürzt.

Um sicherzugehen, dass ich die vorgeschlagene Lösung nicht übersehen habe, finden Sie hier den web.Config-Auszug. Ich habe versucht, das behaviourConfiguration-Attribut aus dem Tag services / service / endpoint zu entfernen ohne die Seite zu verlassen.

<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>

Danke im Voraus

Antworten auf die Frage(1)

Ihre Antwort auf die Frage