Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在尝试获取JQuery帖子以与WCF通信,但不接受JSON数据_Javascript_Ajax_Wcf_Jquery - Fatal编程技术网

Javascript 正在尝试获取JQuery帖子以与WCF通信,但不接受JSON数据

Javascript 正在尝试获取JQuery帖子以与WCF通信,但不接受JSON数据,javascript,ajax,wcf,jquery,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, "

我正在使用以下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>
与我传递用户数据时相反,此时它会给出POST 400(错误请求)错误

我确信这与JSON对象userInfo的构建或解释方式有关,如果有帮助的话,我可以发布Fiddler 2或WireShark转储。让我知道

我真的无法更改服务的WCF端,所以希望在我这方面我能做些什么来实现这一点

谢谢

编辑

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

服务器在处理请求时遇到错误。异常消息为“传入消息>具有意外消息格式“Raw”。操作所需的消息格式为“Xml”>“Json”。这可能是因为尚未在绑定上配置WebContentTypeMapper。有关详细信息,请参阅WebContentTypeMapper的>文档。“。有关详细信息,请参阅服务器日志。>异常堆栈跟踪是:

在System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(消息消息,对象[]参数)在System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(消息,对象[]参数)在System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializerRequest在System.ServiceModel.Dispatcher.DispatcherOperationRuntime.System.ServiceModel.Dispatcher.DispatcherOperationRuntime.InvokeBegin(MessageRpc&rpc)在System.ServiceModel.Dispatcher.ImmutableDispatcherRuntime.ProcessMessage5(MessageRpc&rpc)上的(消息消息消息消息,对象[]参数)位于System.ServiceModel.Dispatcher.ImmutableDispatcheRuntime.ProcessMessage41(MessageRpc&rpc)的System.ServiceModel.Dispatcher.MessageRpc.Process(布尔等操作上下文集)

因此,我想我需要弄清楚如何通过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; }
}

在服务的接口中,您需要具有OperationalContract属性,并且在该属性中,您需要设置RequestFormat

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

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

有人试图通过提到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);
        }});

请发布您的WCF服务配置。不幸的是,我没有访问权限…我已尝试实施您的建议,但似乎不起作用…
[OperationContract,WebInvoke(Method=“post”,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)]bool登录(XiineUserInformation用户);
[OperationContract, WebInvoke(UriTemplate = "/runGenericMethodJSON", Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedRequest)]
// 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);
        }});