gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:16:48 28 4
gpt4 key购买 nike

我正在使用以下 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(serviceUrl , loginSuccess);

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

$.post(serviceUrl , userInfo, loginSuccess);

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

我真的无权更改服务的 WCF 端,所以希望我可以做一些事情来完成这项工作。

谢谢!

编辑

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

The server encountered an error processing the request. The exception message is 'The incoming message >has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', >'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the >documentation of WebContentTypeMapper for more details.'. See server logs for more details. The >exception stack trace is:

at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

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

更多信息 --- 再次……

好的...没有 app.config 或 web.config 本身。

这是我能得到的契约(Contract)和代码等等。

[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请求,看看实际发送和接收的是什么,观察到我不仅需要对数据进行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);
}});

关于javascript - 试图让 JQuery Post 与 WCF 通信,但不接受 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6601349/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com