2

I have a web service which returns a string currently and I want it to return a string array. My js code looks like this:

function getMenu(menuID) {
    $("#open").css('display', 'block');
    $("#close").css('display', 'none');
    $('#flash_container').flash({
        swf: 'mainGallery.swf',
        width: "100%",
        height: 330,
        wmode: "transparent",
        quality: "high",
        flashvars: { XMLFile: 'xml/' + menuID + '.xml' }
    });

    $("div#panel").slideUp("slow", function() {
        var language;
        if (getParameterByName('language') == 'en')
            language = 1;
        else
            language = 0;

        GetMenu.CreateMenu(menuID, language, OnGotMenu, OnFail, "XmlDocument");

    });

}

function OnGotMenu(result) {
    var lblOutput = document.getElementById("lblOutput");
    lblOutput.innerHTML = result;

    $(".MenuList a:last").removeAttr('border-left');

    $("div#panel").slideDown("slow");
    $("#open").css('display', 'none');
    $("#close").css('display', 'block');
    fleXenv.fleXcrollMain("panel_content");
}

Now I return a string. I want to return string[].

How do I need to modify my js code to use this array?

Edit: Here is the c# code after the change proposed by @pukipuki (only the relevant part)

table.RenderControl(tw);
        string retVal = "[\"" + menuDIV + sb.ToString() + "\", \"" + currTitle + "\"]";
        return retVal.ToString();

Maybe I'm doing something wrong here.

Edit 2: Better version of the c# code:

table.RenderControl(tw);
    retArray[0] = menuDIV + sb.ToString();
    retArray[1] = currTitle;
    string retVal = jsSer.Serialize(retArray);
    return retVal;

But I still don't get an array I can access like:

var that = result[0];
var oranother = result[1];

etc.

6
  • where is the part when you call the web service in your code? Commented Jul 11, 2011 at 9:02
  • I call it via asp.net ajax. GetMenu.CreateMenu() Commented Jul 11, 2011 at 9:08
  • find a proper .ToJson() library for your ASP.NET code because what you're currently doing is very prone to errors Commented Jul 11, 2011 at 9:35
  • @keymone - Already considered this. Posting another version of the code. Commented Jul 11, 2011 at 9:43
  • what do you get? can you post responseText from javascript onSuccess callback? Commented Jul 11, 2011 at 9:47

2 Answers 2

1

You should return data in format like:

[ "array", "elements", "here" ]

and then you will be able to index through result.

Sign up to request clarification or add additional context in comments.

1 Comment

Tried it. It does not work. I add the c# code to my post. Maybe the issue is there.
0

Thank you all for your help, you pushed me in the right direction. The solution was to add [ScriptMethod(ResponseFormat=ResponseFormat.Json)]

Then I just return the string array as-is and the framework converts it to JSON. Back in the js code I can now just access the fields using result[0], result[1], etc.

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.