0

Please see the JavaScript code below:

function SaveOrReview(Me)
    {
        var frm = document.forms[0];
        var arrayDisposals;
        var intCount;
        var arrayDisposals = new Array();
        for (i = 0; i < frm.elements.length; i++) {
            if (frm.elements[i].type == "checkbox" && frm.elements[i].name.substr(0, 3) == "Del") {
                arrayDisposals.push(frm.elements[i].id)
            }
        }

        $.ajax({
            type: "POST",
            url: "PrimaryNominalAjax.aspx/AddDisposalList",
            data: '{str: "' + arrayDisposals + '", strCon: "' + $("#<%=fieldGenieConnectionString.ClientID%>")[0].value + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess(),
            failure: function (response) {
                alert('there was an error updating a MOPI grade')
            }

        });

        function OnSuccess() {
            return function (response) {
                alert('succeeded')
            }
        }
    }

and the server side code below:

<System.Web.Services.WebMethod()> _
    Public Shared Function AddDisposalList(ByVal str() as string, ByVal strCon As String) As String
   msgbox("test")
end function

The webmethod is not called. If I remove the array argument from the JavaScript call and the string array from the calling JavaScript then it works i.e. the message box display. Therefore my question is how do you pass an array over AJAX.

I have looked at a few other questions on here but they relate to PHP and Java, rather than VB.NET.

3
  • you can try data: '{str: "' + JSON.stringify(arrayDisposals) + '", strCon: "' + $("#<%=fieldGenieConnectionString.ClientID%>")[0].value + '"}', but i don't know how you get it in server side with VB.NET. Get array like string and parse it Commented Aug 6, 2014 at 10:35
  • @Engin Üstün, thanks. what is the argument on the server (webmethod). Is it an array or list etc? +1. Commented Aug 6, 2014 at 10:43
  • argument on the server is string, you get it and parse like array. Commented Aug 6, 2014 at 12:25

1 Answer 1

2

You'd want to JSON.stringify() it (to turn it in to JSON) before passing it in to the ajax call.

Something like:

data: '{str: ' + JSON.stringify(arrayDisposals) + ', strCon: "' + $("#<%=fieldGenieConnectionString.ClientID%>")[0].value + '"}',
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. What is the argument on the server (WebMethod). Is it an array or list etc. +1.
I'd guess an array (since thats what JSON is storing it as) - although that is only a guess. May be worth just trying it as both and see which works?

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.