0

Okay I am serializing a bunch of years from a database call into a json object. This object is the response from the webservice to the first ajax call. My javascript error console throws an error on the line where it is suppose to deserialize it. I am trying to figure out what is wrong.

Update:

This code works, thanks to Jussi Kosunen

    $.ajax(
        {
            type: "POST",
            url: "default.aspx/HelloWorld",
            dataType: "json",
            data: "{name:'" + name + "'}",
            contentType: "application/json; charset=utf-8",
            success: function (msg) {

                //parse the object into something useable.
                var stringarray = JSON.parse(msg.d);

                //empty the results for next time around.
                 $('#year').empty();
                 for (index in stringarray) {
                    $('#year').append('<option>' + stringarray[index] + "</option>");


                    alert(stringarray[index]);

                }

This is the C# that serialized the list into an json object;

    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public static string HelloWorld(string name)
    {
        string splitme = "USE VCDB SELECT DISTINCT YearID  FROM BaseVehicle";
         DataTable dt =  getDataTable(splitme);
         List<string> ids = new List<string>();
         foreach (DataRow row in dt.Rows)
         {
            ids.Add(row.ItemArray[0].ToString());

         }
        JavaScriptSerializer js = new JavaScriptSerializer();

        string x =js.Serialize(ids);

        return x;
    }

Now when I go into debug. this is the string the C# is returning.

     [\"1896\",\"1897\",\"1898\",\"1899\",\"1900\",\"1901\",\"1902\",\"1903\",\"1904\",\"1905\",\"1906\",\"1907\",\"1908\",\"1909\",\"1910\",\"1911\",\"1912\",\"1913\",\"1914\",\"1915\",\"1916\",\"1917\",\"1918\",\"1919\",\"1920\",\"1921\",\"1922\",\"1923\",\"1924\",\"1925\",\"1926\",\"1927\",\"1928\",\"1929\",\"1930\",\"1931\",\"1932\",\"1933\",\"1934\",\"1935\",\"1936\",\"1937\",\"1938\",\"1939\",\"1940\",\"1941\",\"1942\",\"1943\",\"1944\",\"1945\",\"1946\",\"1947\",\"1948\",\"1949\",\"1950\",\"1951\",\"1952\",\"1953\",\"1954\",\"1955\",\"1956\",\"1957\",\"1958\",\"1959\",\"1960\",\"1961\",\"1962\",\"1963\",\"1964\",\"1965\",\"1966\",\"1967\",\"1968\",\"1969\",\"1970\",\"1971\",\"1972\",\"1973\",\"1974\",\"1975\",\"1976\",\"1977\",\"1978\",\"1979\",\"1980\",\"1981\",\"1982\",\"1983\",\"1984\",\"1985\",\"1986\",\"1987\",\"1988\",\"1989\",\"1990\",\"1991\",\"1992\",\"1993\",\"1994\",\"1995\",\"1996\",\"1997\",\"1998\",\"1999\",\"2000\",\"2001\",\"2002\",\"2003\",\"2004\",\"2005\",\"2006\",\"2007\",\"2008\",\"2009\",\"2010\",\"2011\",\"2012\",\"2013\"]

Big Ugly String

Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61
  • 1
    jQuery is likely parsing it for you automatically, make sure msg is actually a string and not an array or object. – Jussi Kosunen Aug 21 '13 at 20:49
  • 1
    refer to this question: http://stackoverflow.com/questions/14432165/uncaught-syntaxerror-unexpected-token-with-json-parse – Agustin Meriles Aug 21 '13 at 20:51
  • @ Jussi Kosunen It was an object. before I tried to parse it, it went straight to the alert and was called [object Object]. How do I operate on it then? – Alexander Ryan Baggett Aug 21 '13 at 20:54
  • You probably over-JSON-ifying your response... It is marked as `ResponseFormat.Json` and in addition you are returning JSON in that string result. – Alexei Levenkov Aug 21 '13 at 20:57
  • OH.....interesting... Just tested removing the response format. It doesn't appear to do me any good. Hmmm I wish I could just send a string [] directly. – Alexander Ryan Baggett Aug 21 '13 at 21:02
  • @Jussikosunen What you said was spot on. Now I have my response in a big ugly string. I want to give you points if I can. My next step is to try to turn that ugly well-escaped string into an array. I can either turn that into its own question or edit this one. – Alexander Ryan Baggett Aug 21 '13 at 21:24
  • I'll post it as an answer if you want to mark it as answered. Note that JSON.parse also handles arrays (e.g. `"[\"foo\",\"bar\"]"`) if that's what you mean by ugly well-escaped string. – Jussi Kosunen Aug 21 '13 at 21:29

1 Answers1

1

As you're passing dataType: "json" into your $.ajax call, it's parsing your JSON automatically.

Jussi Kosunen
  • 8,277
  • 3
  • 26
  • 34
  • To get it into a string on the other end in Javascript I needed to have var deserialized = JSON.parse(msg); be var deserialized = JSON.stringify(msg); – Alexander Ryan Baggett Aug 21 '13 at 21:31
  • Yup, although strictly speaking that would be `var serialized = JSON.stringify(msg)`. Alternatively if you're sure you want it as a string, you can specify `dataType: "text"` in your request, then jQuery will skip the deserialization. – Jussi Kosunen Aug 21 '13 at 21:34
  • What do you recommend most? I am trying to put these values into options for a – Alexander Ryan Baggett Aug 21 '13 at 21:36
  • Okay quick update. Using dataType:text is the same is using JSON.stringify later. (I just tested the results). I need to know how to get the string into something usable. – Alexander Ryan Baggett Aug 21 '13 at 21:40
  • 1
    If you skip the JSON.parse in your original post you should be able to loop through the data in `msg`, or possibly `msg.d` as the screenshot would suggest. So just `for(var i=0;i – Jussi Kosunen Aug 21 '13 at 21:40
  • That sounds like its worth a shot. I will try it. – Alexander Ryan Baggett Aug 21 '13 at 21:41
  • for (x in msg.d) { alert(msg.d[x]); } this is close. But it goes only 1 character at a time. so I get [ " 1 8 9 6 " , If you know how to have it go entire strings at a time, that would be excellent. Otherwise I will be doing a lot of tricky character parsing. – Alexander Ryan Baggett Aug 21 '13 at 21:59
  • 1
    Aah, I just noticed that d is a string rather than an array, sorry about that. With your existing data you can do `JSON.parse(msg.d)` to get the array, but before that try returning your `List` instead of a serialized string in the C# method - the `ResponseFormat = ResponseFormat.Json` should make msg the array directly. – Jussi Kosunen Aug 22 '13 at 05:59
  • Returning the List did not work, But, doing JSON.parse(msg.d) and then looping through each variable like so for (x in gooober) { alert(gooober[x]); } x ends up being the index. But that works. – Alexander Ryan Baggett Aug 22 '13 at 14:04