I have simplified my code to just passing the array but still am not having any luck
When I step through the code and get to the point of the ajax request
jsonText contains:
[{"UserId":"8"},{"UserId":"9"},{"UserId":"5"},{"UserId":"13"},{"UserId":"6"},{"UserId":"11"}]
and
jsonTextSerialized contains:
"[{\"UserId\":\"8\"},{\"UserId\":\"9\"},{\"UserId\":\"5\"},{\"UserId\":\"13\"},{\"UserId\":\"6\"},{\"UserId\":\"11\"}]"
function GetUserSchedules() {
var jsonText = $.toJSON(arrParams);
var jsonTextSerialized = Sys.Serialization.JavaScriptSerializer.serialize(jsonText);
$.ajax({
type: "POST",
url: "/myurl/jquery.aspx/GenerateUserSchedules",
data: "{'data':'" + jsonTextSerialized + "'}",
contentType: "application/json",
dataType: "json",
success: function () { alert('Made It!'); },
error: function (result) { alert(Failed: ' + result.responseText);
});
My code behind has
[Serializable]
public class User
{
public int UserId { get; set; }
}
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod]
public static void GenerateUserSchedules(User[] data)
{
//do stuff
}
The responseText is:
"There was an error processing the request.","StackTrace":"","ExceptionType":""}
What am I doing wrong?
MY SOLUTION WITH YOUR HELP:
Thank you all for your efforts. I can't express how grateful for all your input. I am embarassed to admit it, but I had been stuck on this for days.
I see from all your answers that there are various ways to approach this. I like the JSON.stringify solution best for two reasons:
- It removes the danger of inadvertant typos when I add parameters to the ajax request.
- According to Oleg, it is a more efficient way to serialize the data objects
So here is how I decided to resolve the issue.
<script type="text/javascript">
var startDate;
var endDate;
var ddlViewSelectedItem;
var ddlViewSelectedValue;
var ddlOrgSelectedValue;
var arrUsers= [];
$(document).ready(function () {
ddlViewSelectedItem = $('#<%=ddlView.ClientID %> option:selected').text();
ddlViewSelectedValue = $('#<%=ddlView.ClientID %> option:selected').val();
ddlOrgSelectedValue = $('#<%=ddlOrganization.ClientID %> option:selected').val();
$.when(GetStartDate(), GetEndDate()) //these populate strt and end dates
.then(function () {
GetUserIDs(); // populates arrUsers
GetUserSchedules();
})
.fail(function () {
failureAlertMsg();
})
});
// Here I use JSON.stringify because it simplifies adding params. No messy single and/or double quote confusion. I love this. Must include json2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js
function GetUserSchedules() {
var jsonTextStringified = JSON.stringify({ data: arrParams, startDate: startDate, endDate: endDate, ddlViewSelectedItem: ddlViewSelectedItem, ddlViewSelectedValue: ddlViewSelectedValue, ddlOrgSelectedValue: ddlOrgSelectedValue });
$.ajax({
type: "POST",
url: "/myurl/jquery.aspx/GenerateUserSchedules", // this is a call to a pagemethod, not a webservice, so .aspx is correct
data: jsonTextStringified,
contentType: "application/json",
dataType: "json",
success: function () { alert('Sweet! Made It!'); }
,
error: function (result) { alert('Failed!: ' + result.responseText); }
});
}
Code behind:
[Serializable]
public class User
{
public string UserId { get; set; }
}
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod]
public static void GenerateUserSchedules(User[] data, string startDate, string endDate, string ddlViewSelectedItem, string ddlViewSelectedValue, string ddlOrgSelectedValue)
{
//do cool stuff and eventually send data back
}
Thank you again for all your help