javascript - Uncaught InvalidStateError in JSON.stringify - Stack Overflow

I've read this other thread and it was no good: Put data into JSON with JqueryWhenever I try to JS

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.

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.

Share Improve this question edited May 23, 2017 at 12:11 CommunityBot 11 silver badge asked Sep 3, 2013 at 20:32 AnimaSolaAnimaSola 7,85614 gold badges47 silver badges67 bronze badges 12
  • 1 What browser is giving you this error? – Pointy Commented Sep 3, 2013 at 20:34
  • 1 Sounds like a cross-frame reference that got garbage-collected. Are you using anything such? – Bergi Commented Sep 3, 2013 at 20:38
  • @Bergi huh? I don't think that's my post. – AnimaSola Commented Sep 3, 2013 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 Commented Sep 3, 2013 at 20:41
  • @AnimaSola is the "partial view" in an <iframe>? Since each <iframe> is a distinct JavaScript global environment, when objects are handed between frames then odd things can happen. – Pointy Commented Sep 3, 2013 at 20:55
 |  Show 7 more ments

2 Answers 2

Reset to default 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()
        });

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744379455a4571353.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信