3

I've been dabbling in PageMethods (in Webfroms) for a Web App I'm developing. I hit a wall when it came to master pages (ie. PageMethods coming back 'undefined'). So, I've changed my approach and am looking at calling a method inside a Web Service instead. (There seems to be a few advantages over PageMethods as well.)

I'm having a bit of difficulty when it comes to getting array information back from the C# code inside the Web Service into Javascript (so it can be used on the client side). The code in the web service (for concept testing) is:

[WebMethod]
    public int[] HelloWorld()
    {
        return new int[] { 1, 2, 3};
    }

The Javascript calling the C# method inside the Web Service is:

 function processSourceInputButtonClicked() {
                            $.ajax({
                                type: "POST",
                                url: "Webservice.asmx/HelloWorld",
                                data: "{}",
                                dataType: "text",
                                success: function (data) {
                                   alert("Ok:" + data);
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    alert("Err:" + textStatus + "," + errorThrown.toString());
                                }
                            });
                        }; 

If I look at 'data', the array comes back as a bunch of XML:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfInt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <int>1</int>
  <int>2</int>
  <int>3</int>
</ArrayOfInt>

Is there a way of converting this HTML into a Javascript array of integers?

4
  • you need to parse xml into json Object for getting data as js array Commented Feb 25, 2015 at 6:21
  • Change the dataType to JSON in your AJAX call, and you can use the JavaScriptSerializer class in C# to convert the array to JSON. Commented Feb 25, 2015 at 6:22
  • I've changed the datatype to "json" and modified the C# code to reflect your suggestion with the JavaScriptSerializer: Commented Feb 25, 2015 at 6:39
  • I've changed the datatype to "json" and modified the C# code to reflect your suggestion with the JavaScriptSerializer so that HelloWorld returns a string (by "return jss.Serialize(new int {1, 2, 3});). Now I get a parsererror, SyntaxError: Invalid character. Have I applied your suggestions correctly? Commented Feb 25, 2015 at 6:45

2 Answers 2

1

You should change the "dataType" to "json" like this:

$.ajax({
                                type: "POST",
                                url: "Webservice.asmx/HelloWorld",
                                data: "{}",
                                dataType: "json", // change "text" to "json"
                                success: function (data) {
                                   alert("Ok:" + data);
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    alert("Err:" + textStatus + "," + errorThrown.toString());
                                }
                            });
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your suggestion. If I just change the datatype to "json" (and do no other changes), I get a parseerror, SyntaxError: Invalid character. Is there something in the C# code that needs to be modified? Does returning a set of integers (as opposed to a string) cause problems when using Ajax?
try to add this attribute to your WebMethod:[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
I've given that a try - added the decoration you suggested. I still get the same error. Maybe the Ajax call is looking for more?
Got it! @ronen's suggestion works provided you have contentType: "application/json; charset=utf-8" inside the Ajax call. (To get the actual integer array in Javascript you want to look in the '.d' property of data.)
1

First you need to parse the xml and find the int nodes.

Then you can iterate over the collection and do as you wish.

Here is a simple example that adds the values to an unordered list:

var xml = '<?xml version="1.0" encoding="utf-8"?><ArrayOfInt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"><int>1</int><int>2</int>  <int>3</int></ArrayOfInt>';
  xmlDoc = $.parseXML( xml ),
  $xml = $( xmlDoc ),
  $ints = $xml.find( "int" );

$.each($ints, function( index, value ) {
  $( "ul" ).append('<li>' + $(value).text()  + '</li>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul></ul>

1 Comment

Outstanding ... that'll work. For other who come across this, to get the actual number value out of 'value' over the iteration I used 'value.textContent' and then 'pushed' that onto a Javascript array.

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.