0

I have a list of objects in JavaScript which I need to pass to ASP.NET WebService.

Currently I include ScriptManager into my page:

        <asp:ScriptManager runat="server">
            <Services>
                <asp:ServiceReference Path="/ws_data.asmx" />
            </Services>
        </asp:ScriptManager>

declare WebMethod:

[WebMethod(true)]
public object SaveData(my_package.MyObject[] objects)
{
    //  ...
}

And call the method from JavaScript:

    var parameters = '['+
            + '{"PlayerId":11, "Pos":12, "Flang":13"},'
            + '{"PlayerId":21, "Pos":22, "Flang":23"}'
            + ']';
    ws_data.SaveData(parameters, OnComplete, OnError, OnTimeOut);

When method is called, i receiver 'OnError' called with error message:

Cannot convert object of type 'System.String' to type 'my_package.MyObject[]'

What is wrong in my code?

I'm not too experienced with neither of used technologies (JSON, web services, JavaScript) and lost in different assumption.

Please advise, any help is welcome!

P.S. Changed formatting to match JSON specification (advised by codenoire):

var parameters = [
    {"PlayerId":11, "Pos":12,"Flang":13},
    {"PlayerId":21, "Pos":22,"Flang":23 }
    ];

That helped to make workable simplified case when only 1 object is passed, but passing an array still gives the same error.

1 Answer 1

1

Try this:

var parameters = [{"PlayerId":11, "Pos":12,"Flang":13}, {"PlayerId":21, "Pos":22,"Flang":23 }];
Sign up to request clarification or add additional context in comments.

5 Comments

You mean without external brackets? Will try
It works for simplified case when I pass only one object into method that waits for an object. But that doesn't work for array. With array I still receive an error: 500 (Internal Server Error) ... dunno why
Heh, it does work! The problem is I used some function which generated me a string representation of my array, I should call eval(...) to transform it in object...
The example I gave you is using an array.
Yes, that's what I've written: in my code I used a string... so needed to add 'eval()'

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.