I am trying to pass a multi-layered array in the form:
array[<string>, array[<string>, <string>]]
using jquery ajax
var cssarray = [];
function SaveChanges(element, updates) {
var exists = false;
var updated = false;
//need to check if already present and if so overwrite the changes.
for (var i = 0; i < cssarray.length; i++) {
if (cssarray[i][0] === element) {
exists = true;
var existingupdates = cssarray[i][1];
for (var j = 0; j < updates.length; j++) {
for (var k = 0; k < existingupdates.length; k++) {
if (existingupdates[k][0] === updates[j][0]) {
existingupdates[k][1] = updates[j][1];
updated = true;
}
}
if (!updated)
existingupdates.push(updates[j]);
}
cssarray[i][1] = existingupdates;
}
}
if(!exists)
cssarray.push([element, updates]);
}
runAjax('/Public/AdminServices.asmx/UpdateCSS', 'updates', cssarray)
.done(
function (__exists) {
if (__exists.d) {
alert(__exists.d);
}
else {
alert("hmmm error");
alert(__exists.d);
}
}
)
.fail(
function (jqXHR, textStatus, errorThrown) {
alert(errorThrown + ':' + textStatus + ':' + jqXHR.responseText);
}
);
function runAjax(webmethod, fieldname, fieldtext) {
var jsonObject = {};
jsonObject[fieldname] = fieldtext;
return $.ajax({
type: "POST",
url: webmethod,
data: JSON.stringify(jsonObject),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}
I want to pass it across to my asmx method
[WebMethod(EnableSession = true)]
[SoapHeader("authentication")]
public string UpdateCSS(string updates)
{
//add is master check so only admin will be able to change the website
if (User.Identity.IsAuthenticated &&
!string.IsNullOrEmpty(Convert.ToString(Session["IsMaster"])))
{
JavaScriptSerializer json = new JavaScriptSerializer();
List<KeyValuePair<String, List<KeyValuePair<String, String>>>> css = json.Deserialize<List<KeyValuePair<String, List<KeyValuePair<String, String>>>>>(updates);
AdminFunctions.UpdateStyleSheet(css);
return "User is a valid user";
}
else
{
return "changes not committed as the user is not authenticated.";
}
}
But it is throwing an unable to cast error on this object? the error being thrown is as follows;
"Type "System.String" is not supported for deserialization of an array.","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList\u0026 convertedList)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary
2 rawParams)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
any ideas why?
Thanks in advance.