0

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(IDictionary2 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.

2 Answers 2

1

I would use Dictionary> I did something similar not so long ago. Or try a nested NameValueCollection

Change

JavaScriptSerializer json = new JavaScriptSerializer();
List<KeyValuePair<String, List<KeyValuePair<String, String>>>> css = json.Deserialize<List<KeyValuePair<String, List<KeyValuePair<String, String>>>>>(updates);

to

JavaScriptSerializer json = new JavaScriptSerializer();
Dictionary<String, Dictionary<String, String>> css = json.Deserialize<Dictionary<String, Dictionary<String, String>>>(updates);
Sign up to request clarification or add additional context in comments.

2 Comments

Matt, thats fine but the issue is casting it on the server side thats throwing the error. I have tried JavaScriptSerializer json = new JavaScriptSerializer(); List<KeyValuePair<String, List<KeyValuePair<String, String>>>> css = json.Deserialize<List<KeyValuePair<String, List<KeyValuePair<String, String>>>>>(updates); bit this too thrown an error when casting? Would you be able to provide a copy of the working code you used when casting it?
You need to update your question with the exception you're getting
0

ok the issue was that I neede to stringify the sub array so the following code fixed the issue script side.

JSON.stringify({ updates: JSON.stringify(fieldtext) })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.