4

I've read this other thread and it was no good: Put data into JSON with Jquery

Whenever I try to JSON.stringify an object array I get an error saying:

Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable. 

Here is my JS:

            var responseItems = [];

            var keynoteContainer = $('div.keynote-questions');
            var eventQuestionContainer = $('div.event-questions');
            var sessionContainer = $('div.session-questions');
            var eventId = $('#Evaluation-Event-Id').val();

            keynoteContainer.children().each(function (index, el) {
                var element = $(el);

                var id = "-1";
                var parentId = element.find('input[type=hidden]').val();
                var parentType = "Keynote";
                var responseValue = element.find('.response-item-slider').slider("option", "value");
                var responseText = "";

                var response = { "Id": id, "EventId": eventId, "ParentId": parentId, "ParentType": parentType, "ResponseValue": responseValue, "ResponseText": responseText };
                responseItems.push(response);
            });                    

            eventQuestionContainer.children().each(function (index, el) {
                var element = $(el);

                var id = "-1";
                var parentId = element.find('input[type=hidden]').val();
                var parentType = "EventQuestion";
                var responseValue = element.find('.response-item-slider').slider("option", "value");
                var responseText = element.find('textarea').val();

                var response = { "Id": id, "EventId": eventId, "ParentId": parentId, "ParentType": parentType, "ResponseValue": responseValue, "ResponseText": responseText };
                responseItems.push(response);
            });

            sessionContainer.children().each(function (index, el) {
                var element = $(el);

                var id = "-1";
                var parentId = element.find('input[type=hidden]').val();
                var parentType = "Session";
                var responseValue = element.find('.response-item-slider').slider("option", "value");
                var responseText = "";

                var response = { "Id": id, "EventId": eventId, "ParentId": parentId, "ParentType": parentType, "ResponseValue": responseValue, "ResponseText": responseText };
                responseItems.push(response);
            });

            responseItems = JSON.stringify(responseItems);

I've tried to log everything and it only breaks at the last line where I stringify it.

How can I fix this? Any piece of advise or information would be highly appreciated.

Community
  • 1
  • 1
AnimaSola
  • 7,146
  • 14
  • 43
  • 62
  • 1
    What browser is giving you this error? – Pointy Sep 03 '13 at 20:34
  • 1
    Sounds like a cross-frame reference that got garbage-collected. Are you using anything such? – Bergi Sep 03 '13 at 20:38
  • @Bergi huh? I don't think that's my post. – AnimaSola Sep 03 '13 at 20:38
  • @Bergi Hmm... you may be on to something there. I've got a page which loads a partial view (evaluation items) into a DIV. I then go through each item in that div to build the responseItems object array. it works fine (logged everything) up until the stringify where I get that error. – AnimaSola Sep 03 '13 at 20:41
  • @AnimaSola is the "partial view" in an ` – Pointy Sep 03 '13 at 20:55
  • @Pointy nope. It's loaded onto a div via $('divname').html(viewdata). viewdata is html returned from a controller action. – AnimaSola Sep 03 '13 at 20:56
  • Who added the "may already have an answer" post? I mentioned on my first line that it was no good! – AnimaSola Sep 03 '13 at 21:02
  • @AnimaSola Just try removing properties one by one until serialization works. You should quickly find the issue. – plalx Sep 03 '13 at 21:04
  • @plalx could it be one of the properties being undefined? because I've got a scenario where it could be either a textvalue or slidervalue and I just put both. when the object is added/made, the property has a value of undefined. I just imagined it would turn into null once serialized. could this be the problem? – AnimaSola Sep 03 '13 at 21:05
  • @AnimaSola, I dont think so. `undefined` values are ignored. Try this code to find the erroneous property. It will go through the array, remove one property at a time and try to stringify: `Object.keys(responseItems[0]).forEach(function (key, index) { var error = false; responseItems.forEach(function (item) { delete item[key]; }); try { JSON.stringify(responseItems); } catch (e) { error = true; } if (!error) { throw 'erroneous property: ' + key; } });` – plalx Sep 03 '13 at 22:35
  • I got it working, it was definitely the undefined property that fixed it. Defined both as empty strings first rather than a value from an element. – AnimaSola Sep 03 '13 at 22:42
  • @AnimaSola, That's wierd, I still doubt it's because of undefined values. `JSON.stringify({test: undefined});` works well for me. – plalx Sep 04 '13 at 13:15

2 Answers2

4

I had the same error. In my case, I had forgotten the .val() when I was setting one of the model properties. There wasn't an error until I tried to JSON.stringify the model

        this.model.set(
        {
            customername: this.$("#customername").val(),
            jobtitle: this.$("#jobtitle"),  //This was the line causing the error
            testimonialtext: this.$("#testimonialtext").val()
        });
Craig Howard
  • 1,649
  • 15
  • 10
  • Had the same issue. I had set `$tr.find($('.asm-note').val())` to a property in the object, when what I actually meant was `$tr.find('.asm-note').val()` – thameera Mar 09 '14 at 17:00
2

I had the same problem. In my case it was fixed casting my trouble variable toArray(). I mean something like this:
responseItems = responseItems.toArray();
I don't know why that variable looks like an Array but it isn't after that you can call:
JSON.stringfy(responseItems);
without problems.
PD: sorry for my english I'm not native english speaker

Carlos
  • 190
  • 8