[英]WebMethod fails to capture object sent via ajax
I have been trying to get an object from the client in JavaScript to send to a C# WebMethod to handle it and process it. 我一直在尝试从JavaScript客户端中获取一个对象,以发送到C#WebMethod进行处理和处理。 So far I have been unable to get the WebMethod to successfully handle the incoming json object. 到目前为止,我还无法获得WebMethod来成功处理传入的json对象。
My JavaScript: 我的JavaScript:
var _Messages;
function callData() {
var timeNow = new Date;
$.ajax({
type: "POST",
url: "Default.aspx/All",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {_Messages = data.d; Render(data.d); $('#updated_time').html("<p class='text'>Last Updated: " + ("0" + timeNow.getHours()).slice(-2) + ":" + ("0" + timeNow.getMinutes()).slice(-2) + ":" + ("0" + timeNow.getSeconds()).slice(-2) + "</p>") },
failure: function (response) {
alert(response.d);
}
});
}
function printFunction() {
var listofmsg = $.grep(_Messages, function(a) {
return a.Text.trim().replace(/\r\n|\r|\n/g, ' ') == $('#navigate tr td').eq(active*2-1).text().trim().replace(/\r\n|\r|\n/g, ' ');
});
listofmsg[0].Text = listofmsg[0].Text.replace(/\r\n|\r|\n/g, ' ');
var msg = listofmsg[0];
//var input = {TimeStamp: msg.Text, TimeSet: msg.TimeSet, Text: msg.Text, Type: msg.Type, IsGreen: msg.IsGreen}
console.log(JSON.stringify({'message':msg}));
$.ajax({
type: "POST",
url: "Default.aspx/PrintMessage",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({ 'message' : msg}),
success: function (data) { console.log(data.d);},
failure: function (response) {
alert(response.d);
}
});
}
My Object: 我的对象:
public class Message
{
public string TimeStamp { get; set; }
public DateTime TimeSet { get; set; }
public string Text { get; set; }
public string Type { get; set; }
public bool IsGreen { get; set; }
public Message(string timestamp, DateTime timeset, string text, string type, bool isgreen)
{
TimeStamp = timestamp;
TimeSet = timeset;
Text = text;
Type = type;
IsGreen = isgreen;
}
}
My WebMethod: 我的WebMethod:
[WebMethod]
public static bool PrintMessage(Message message)
{
try
{
...
return true
}
catch (Exception ex)
{
...
return false
}
}
My Error is that when I reach the WebMethod the message object is null. 我的错误是,当我到达WebMethod时,消息对象为null。 Despite it being filled on the client side like so: 尽管它像这样在客户端填充:
{"message":[{"TimeStamp":"08:39:28","TimeSet":"/Date(1398727057151)/","Text":"TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST ","Type":"spc","IsGreen":true}]}
Any ideas what is going wrong? 任何想法出了什么问题? My best guess is that I am not formatting my json object correctly, 我最好的猜测是我没有正确格式化json对象,
Adjust your ajax post to use 调整要使用的ajax帖子
$.ajax({
type: "POST",
url: "/PrintMessage",
contentType: "application/json",
data: JSON.stringify({ 'message' : msg}),
success: function (data) { console.log(data.d);},
failure: function (response) {
alert(response.d);
}
});
Linting the JSON object you have provided (using http://jsonlint.com/ ) and it appears as though "message" is an array/collection and so deserialisation will be a tad off. 整理一下您提供的JSON对象(使用http://jsonlint.com/ ),看起来“消息”是一个数组/集合,因此反序列化将不可行。 You should either change the type in your method to: 您应该将方法中的类型更改为:
public static bool PrintMessage(List<Message> message)
Or remove the array declaration (the [] brackets) in your JSON object declaration and retain the same signature, that is 或删除JSON对象声明中的数组声明([]括号)并保留相同的签名,即
public static bool PrintMessage(Message message)
With the JSON object modified to: 将JSON对象修改为:
{ "message": { "TimeStamp": "08:39:28", "TimeSet": "/Date(1398727057151)/", "Text": "TEST TEST TEST TEST TEST", "Type": "spc", "IsGreen": true } }
I'm wondering if the property "message" is even needed actually. 我想知道是否实际上甚至需要“消息”属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.