试图让JQuery Post与WCF通信,但JSON数据不被接受

Joe*_*Joe 6 javascript ajax wcf jquery

我使用以下JQuery\JavaScript代码与WCF 4 REST服务进行通信.

<script>
var serviceUrl = "http://services.xiine.com/Xiine/Live/AccountService/rest/json/Login";
var userInfo = {
    "IsNotEncrypted":true,
    "Password":null,
    "UserName":null
};

var loginSuccess = function(data, textStatus, jqXHR){
console.log("yay");
};
var loginError = function(){
console.log("oh no");
};

$.post(serviceUrl , userInfo, loginSuccess);
$.post(serviceUrl , loginSuccess);

</script>
Run Code Online (Sandbox Code Playgroud)

我试图确定为什么当我不传递用户数据时服务将正确返回false值:

$.post(serviceUrl , loginSuccess);
Run Code Online (Sandbox Code Playgroud)

与我传递用户数据时相反,此时它会发出POST 400(错误请求)错误.

$.post(serviceUrl , userInfo, loginSuccess);
Run Code Online (Sandbox Code Playgroud)

我确信它与如何构建或解释JSON对象userInfo有关,我可以发布Fiddler 2或WireShark转储,如果这将有所帮助.请告诉我......

我真的没有权限更改服务的WCF方面,所以希望我能做的就是让我能够做到这一点.

谢谢!

编辑

我得到了更多信息......显然,问题是服务器响应时出现以下错误:

服务器遇到处理请求的错误.异常消息是"传入消息"具有意外的消息格式"原始".操作的预期消息格式是'Xml',>'Json'.这可能是因为尚未在绑定上配置WebContentTypeMapper.有关更多详细信息,请参阅WebContentTypeMapper的>文档.请参阅服务器日志以获取更多详 >异常堆栈跟踪是:

处于System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息消息,Object []参数)的System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息消息,Object []参数),位于System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(消息消息,对象[]参数)在System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&RPC)在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&RPC)在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&RPC)在System.ServiceModel.Dispatcher.MessageRpc.Process中的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&rpc)(Boolean isOperationContextSet)

所以我想我需要弄清楚如何通过JQuery.post()方法让对象作为JSON对象穿过线路.

更多信息---再次......

好的...没有app.config或web.config,因此.

这是我可以获得的合同和代码以及什么不是.

[ServiceContract]
public interface IAccount
{
[OperationContract]
bool Login(UserInformation user);
}

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AccountService : IAccount
{
public bool Login(UserInformation user)
{
    //stuff...
}
}

[DataContract]
public class UserInformation
{
[DataMember(IsRequired = true)]
public string UserName;

[DataMember(IsRequired = true)]
public string Password;

[DataMember(IsRequired = true)]
public bool IsNotEncrypted;
}

public interface IUserInformation
{
UserInformation UserInformation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 7

所以,我找到了我的问题的答案.

有人试图通过提及JSON.stringify()方法指出我正确的方向,但我花了一点力气才能正确实现它.

最后,我使用WireShark来嗅出http请求,查看实际发送和接收的内容,并观察我不仅需要对数据进行stingify,还需要指定内容类型.

这是最终的代码.

// This is the URL that is used for the service.
var serviceUrl = "http://services.xiine.com/Xiine/Live/AccountService/rest/json/Login";


// This is the JSON object passed to the service
var userInfo = {
    "UserName": null,
    "Password": null,
    "IsNotEncrypted": true
};

// $.ajax is the longhand ajax call in JQuery
$.ajax({
    type: "POST",
    url: serviceUrl,
    contentType: "application/json; charset=utf-8",

    // Stringify the userInfo to send a string representation of the JSON Object
    data: JSON.stringify(userInfo),
    dataType: "json",
    success: function(msg) {
        console.log(msg);
        }});
Run Code Online (Sandbox Code Playgroud)