I have the following webmethod on my asp.net code behind page:
[WebMethod(EnableSession = true)]
public static bool SaveFailureData(SimpleFailureData data)
{
}
SimpleFailureData is defined as the following:
public class SimpleFailureData
{
public int Id { get; set; }
public string Comments { get; set; }
public double Score { get; set; }
public double Adjustment { get; set; }
public List<ShutdownData> ShutdownData { get; set; }
}
public class ShutdownData
{
public int Id { get; set; }
public string Description { get; set; }
public bool CausedShutdown { get; set; }
public string ShutdownType { get; set; }
}
What I am trying to figure out is how to call this webmethod and format my data so that it is projected/parsed into that class correctly so I can use it. I tried sending a json string to the method, but my breakpoint inside the method was never hit (so I assume the method failed to call due to improper data format).
This is the JSON I tried sending and then calling the method:
json = JSON.stringify( {
Comments: comments,
Score: score,
Adjustment: seAdjustmentValue,
ShutdownData: breakdowns //this is an array of shutdown objects
});
PageMethods.SaveFailureData(json, function(data) {
return;
});
But this failed to get inside my method. Any tips on what the JSON format should be so that is properly works for passing a class as a parameter?
Here is the JSON I attempted to send to the method:
{
"Comments":"",
"Score":66.66666666666667,
"Adjustment":0,
"ShutdownData":[{"Id":"401","CausedShutdown":true,"ShutdownType":"NORMAL"}]
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Follow the Below step and you will get your required output
Ajax function Call
function CallAjaxRequest() {
var Simplefailuredata = {};
Simplefailuredata.Id = 1;
Simplefailuredata.Comments = 'Comments-1';
Simplefailuredata.Score = 500.25;
Simplefailuredata.Adjustment = 700.25;
Simplefailuredata.ShutdownData = new Array();
Simplefailuredata.ShutdownData[0] = new Object({ Id: 2, Description: "Desc-1", CausedShutdown: true, ShutdownType: "ShutdownType-1" });
Simplefailuredata.ShutdownData[1] = new Object({ Id: 5, Description: "Desc-2", CausedShutdown: false, ShutdownType: "ShutdownType-2" });
var object = JSON.stringify({ simplefailuredata: Simplefailuredata });
$.ajax({
type: "POST",
url: "Default2.aspx/GetResponse",
contentType: 'application/json; charset=utf-8',
data: object,
dataType: 'json',
cache: false
});
}
<form id="form1" runat="server">
<div>
<input type="button" id="btn" value ="t" onclick="CallAjaxRequest();" />
</div>
</form>
Code Behind
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string GetResponse(simplefailuredata simplefailuredata)
{
return "";
}
}
public class simplefailuredata
{
public int Id;
public string Comments;
public double Score;
public double Adjustment;
public List<shutdownData> ShutdownData;
}
public class shutdownData
{
public int Id { get; set; }
public string Description { get; set; }
public bool CausedShutdown { get; set; }
public string ShutdownType { get; set; }
}
Method 2
ok try this
public static bool SaveFailureData(string sampleFailure)
{
JavaScriptSerializer s = new JavaScriptSerializer();
SimpleFailureData sdata = s.Deserialize<SimpleFailureData>(sampleFailure);
return true;
}
var json = {
"Comments": "",
"Score": 66.66666666666667,
"Adjustment": 0,
"ShutdownData": [{ "Id": "401", "CausedShutdown": true, "ShutdownType": "NORMAL"}]
}
var data = JSON.stringify(json);
$.ajax({
type: "POST",
url: 'Default.aspx/SaveFailureData',
contentType: 'application/json; charset=utf-8',
data: "{'sampleFailure' : '" + data + "'}",
// data: data,
dataType: 'json',
success: function (msg) {
alert(msg.d);
},
error: function (msg) {
alert('Error!');
}
});
});
you will get the data in sdata object.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0