试图让JQuery Post与WCF通信,但JSON数据不被接受
Trying to get a JQuery Post to communicate with WCF, but the JSON data is not being accepted
我使用以下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)]
- 无法使用变量访问数据 JSON
- 使用 ng-repeat解析不均匀数据/ json对象
- 在没有jsonp的情况下从另一个域获取数据(json格式)
- 如何获取带参数的数据json
- 如何在android中显示实时雅虎金融股票数据..json格式如下所示
- 使用 JavaScript 将数据 json 显示到网页中
- 高位图表来源于API数据(JSON)
- 使用动态数据json初始化同位素
- Django:将数据JSON从视图传递给javascript
- 存储配置数据 (json)
- 未捕获的SyntaxError:意外的标识符图像数据json
- 接收数据json/jquery
- Jquery数据表数据json
- 不能在cakephp中使用数组数据json
- 在不锁定浏览器的情况下,将大数据JSON从REST请求反序列化为对象
- 如何读取数据JSON格式数组的字符串数据
- 我有一个流星应用程序,需要从/public/_assets/results/mmresults读取数据.Json文件,在
- 数据json不显示在html
- 使用2个下拉列表创建2个并排的表行来比较数据- JSON
- 需要将复杂的json对象转换为合适的angularjs UI树数据json结构