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

Trying to get a JQuery Post to communicate with WCF, but the JSON data is not being accepted

本文关键字:数据 JSON 通信 JQuery Post WCF      更新时间:2023-09-26

我使用以下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>

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

$.post(serviceUrl , loginSuccess);

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

$.post(serviceUrl , userInfo, loginSuccess);

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

我并没有真正的权限去改变服务的WCF端,所以希望我能在我这端做些什么来让它工作。

谢谢!

编辑

我得到更多的信息…显然,问题是服务器响应以下错误:

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

在System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter

。在System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter中deserializerrequest (Message Message, Object[] parameters)。在System.ServiceModel.Dispatcher.CompositeDispatchFormatter上反序列化请求(Message消息,Object[]参数)。在System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc&在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&在system . servicemodle . dispatcher . immutabledispatchrtime . processmessage5 (MessageRpc&在system . servicemodle . dispatcher . immutabledispatchrtime . processmessage41 (MessageRpc&System.ServiceModel.Dispatcher.MessageRpc. rpc)。过程(布尔isOperationContextSet)

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

更多信息——再次…

好吧…没有app.config或web。

这是我能得到的合同、代码和其他东西。

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

我找到了问题的答案。

有人提到JSON.stringify()方法,试图给我指出正确的方向,但我花了一点努力才使它正确实现。

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

这是最后的代码。

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

在您的服务的接口中,您需要有OperationalContract属性,并且在该属性中,您需要设置RequestFormat。下面是一个示例:

[OperationContract, WebInvoke(UriTemplate = "/runGenericMethodJSON", Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest)]