Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1053
  • Last Modified:

Parsing returned List<KeyValuePair<String,String>> javascript from AJAX call

HI,

I return a List<KeyValuePair<String,String>> form a C# webmethod ajax call.

    runAjax('/Public/AdminServices.asmx/GetFonts', null)
    .done(function (fonts) {
        alert(fonts.d); //alerts Object object Object object

        var html = "<ul class='fontList'>";

        for(var i=0; i<fonts.d.length; i++)
        {
            //both alerting undefined??
            alert(fonts.d[i][0].toString() + fonts.d[i][1].toString());
            html += "<li data-url='" + fonts.d[i][0] + "'>" + fonts.d[i][1] + "</li>";
        }

Open in new window


my webmethod works as follows ( and form debugging is returning 2 lines of values);

HttpWebRequest request = WebRequest.Create(fonts_path) as HttpWebRequest;
        request.Accept = "*/*";
        WebResponse response = request.GetResponse();
        Regex regex = new Regex("(?i)<a href=\"(?<url>.*?)\">(?<text>.*?)</a>");
        List<KeyValuePair<String, String>> files = new List<KeyValuePair<String, String>>();

        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            string result = reader.ReadToEnd();
            MatchCollection matches = regex.Matches(result);

            if (matches.Count == 0)
            {
                Global.log.Info("ERROR: Trying to list fonts from resource directory");
                return null;
            }

            foreach (Match match in matches)
            {
                if (!match.Success) { continue; }

                if (match.Groups["url"].ToString() != "/")
                {
                    Global.log.Info("Debug: MATCH :" + match.Groups["url"].ToString() + ":" +  match.Groups["text"].ToString() + ":");
                    files.Add(new KeyValuePair<String, String>(match.Groups["url"].ToString(),match.Groups["text"].ToString()));
                }
            }
        }

        return files;

Open in new window


any ideas how I can get the two values from the list on the clientside return?
0
flynny
Asked:
flynny
  • 2
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
As mentioned in your other question, try this...
    runAjax('/Public/AdminServices.asmx/GetFonts', null)
    .done(function (fonts) {
        alert(fonts.d); //alerts Object object Object object

        var html = "<ul class='fontList'>";

        for(var i=0; i<fonts.d.length; i++)
        {
            //both alerting undefined??
            alert(fonts.d[i]['Key'].toString() + fonts.d[i]['Value'].toString());
            html += "<li data-url='" + fonts.d[i]['Key'] + "'>" + fonts.d[i]['Value'] + "</li>";
        }

Open in new window

0
 
flynnyAuthor Commented:
thanks as it was off topic from the previous question i opened a new one. Sorry i didnt realised you replied there!
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome!

No worries. I think that I posted in the other question after you had opened this question (which is probably the right thing to do anyway). I just hadn't seen this new question before I posted the reply in the other one.

Oh, BTW, another syntax you could use is.... fonts.d[ i].Key and fonts.d[ i].Value    The .dot notation or the array [subscript] notation should work the same.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now