0

I am trying to display data that is getting returned to me

console.log(data) shows:

{"Data":[[true,true,false],null]}

This is data that ultimately are values of 3 checkboxes.

I am not sure how to troubleshoot why I am getting

Uncaught SyntaxError: Unexpected token u

I gather that u = undefined. Could I be that I need a POST instead? I already tried not having the contentType commented out.
Clearly I have DATA coming back is the $parseJSON(data.d) an issue?

$(document).ready(function () {
        jQuery.support.cors = true;
        $.ajax({
            url: '/GetCheckBox/3521',
            type: 'GET',
            dataType: 'json',
            data: {},
            //contentType: "application/json; charset=utf-8",
            success: function (data) {
                //alert(data);
                console.log(data);
                //WriteResponses(data);
                var objdata = $.parseJSON(data.d);
                WriteResponses(objdata);
            },
            error: function (x, y, z) {
                alert(x + '\n' + y + '\n' + z);
            }
        });

        //Displays in a Table
        function WriteResponses(allNews) {

            alert(allNews);

            var strResult = "<table><th>Name</th><th>Student ID</th><th>Gender</th><th>Age</th>";
            $.each(allNews, function (index, news) {
                strResult += "<tr><td>" + news.StormOut + "</td><td> " + news.StormOut + "</td><td>" + news.StormOut + "</td><td>" + news.StormOut + "</td></tr>";
            });
            strResult += "</table>";
            $("#divResult").html(strResult);
        }

    });

2 Answers2

1

If console.log(data); shows

{"Data":[[true,true,false],null]}

then data is a string (see below for a possible reason why). Why are you trying to parse data.d? Strings don't have a property d. data.d returns undefined and $.parseJSON(undefined) throws the error you are getting.

Use $.parseJSON(data) instead.


FYI, Ben is right in so far that setting dataType: json makes jQuery parse your JSON automatically. However, given the console.log output, it appears that your data is double encoded. I.e. the data is converted to JSON first and the result is again encoded as JSON.

That's wrong of course. You should ensure that the data is only encoded once, and then remove the $.parseJSON call.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • according to answer of Ben's answer `You don't actually need $.parseJSON. jQuery automatically parses it as JSON because you have specified JSON as the data type (dataType: json,).` which is which – guradio Sep 17 '15 at 01:58
  • @Pekka: See my additional note. – Felix Kling Sep 17 '15 at 02:00
  • objData = $.parseJSON(data); alert(objData); --> [object Object] –  Sep 17 '15 at 02:02
  • @BrianThornton: Perfect. Then it works. Or are you confused by the output? `alert` converts the value to a string, and the default string representation of an object is `[object Object]`. See: `alert({foo: 42});`. `alert` is not a debugging tool. `console.log` is. – Felix Kling Sep 17 '15 at 02:02
  • @BrianThornton then felix is right you encoded it twice – guradio Sep 17 '15 at 02:03
  • Since my data looks like this **{"Data":[[true,true,false],null]}** then how can i actually parse that data in a function? –  Sep 17 '15 at 02:05
  • I do use console.log mostly.. So if the data is parsed then how can I use a function to then do a $.each or something? –  Sep 17 '15 at 02:07
  • @BrianThornton: Just pass the value to the function? `myFunction(myValue)`. I don't know what you want to do with the data, so it's up to you to figure that part out. Maybe this helps: http://stackoverflow.com/q/11922383/218196 – Felix Kling Sep 17 '15 at 02:07
  • So like now I am calling `WriteResponses(data)` and that function is giving me --> `Uncaught TypeError: Cannot use 'in' operator to search for '32' in {"Data":[[true,true,false],null]}` –  Sep 17 '15 at 02:09
  • @BrianThornton: it looks you are passing the original string to the function, not the parsed value. You get that error when you are trying to pass a string to `$.each` (or at least so I thought, can't confirm right now). – Felix Kling Sep 17 '15 at 02:11
  • How do I get the parsed value? –  Sep 17 '15 at 02:12
  • @BrianThornton: `$.parseJSON` returns the parsed value (that's what the whole answer is about). – Felix Kling Sep 17 '15 at 02:12
  • Yes, but with `objData = $.parseJSON(data);` results from my function showing `Name Student ID Gender Age undefined undefined undefined undefined ` –  Sep 17 '15 at 02:16
  • @BrianThornton: Well, what else do you expect? When you call `$.each(allNews, ...)` then `index` will be `"Data"` and `news` will be the array `[[true,true,false],null]`. You are trying to access `news.StormOut`, etc, but arrays don't have such a property (proof: `console.log([].StormOut);`), nor does such a property occur anywhere in your data. – Felix Kling Sep 17 '15 at 02:18
  • Thx , I see that news[0] = true,true,false I guess I should play around with this more to separate it out –  Sep 17 '15 at 02:24
  • @BrianThornton: That's why I said you need to figure out what to do with the data. Even if you manage to add it to the table, `Name: true` doesn't make a lot of sense (well, I guess it depends on the UI). – Felix Kling Sep 17 '15 at 02:26
0

You don't actually need $.parseJSON. jQuery automatically parses it as JSON because you have specified JSON as the data type (dataType: json,).

As you can see in your console log, the value of "Data" is returned as an array - in fact, a JavaScript array, not JSON, which would be surrounded by quotes, it's a JSON string.

So, just swap that line with something like:

var objData = data["Data"];

And it should work fine :)

More info at the jQuery docs: http://api.jquery.com/jQuery.getJSON/

Ben Kolya Mansley
  • 1,768
  • 16
  • 24