0

I am currently working with fullCalendar scheduler where I need to drag & drop external events. Here when I am creating a new external event I need to save its data as a JSON string into the localStorage. while page loading I am reading the localStorage and if data is there I need to show the external events in the external drop box from where I can drag them and drop into the calendar. But I am getting "VM872:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1" while trying to parse the JSON string that I stored in localStorage. Below is my code

var extEvent = localStorage.getItem("externalEvents");
console.log(extEvent);

if(extEvent) {
    var parsedItem = JSON.parse(extEvent);
    //var parsedItem = JSON.parse(JSON.stringify(extEvent));
    //var parsedItem = extEvent;
    $.each(parsedItem, function(i, item) {
        var elem = $( "<div class='external-dropable-event fc-event'>" ).appendTo( '#external-events-listing' ).text( item.title );
        elem.draggable({
            helper: function(ev) {
                        return $(ev.target).clone().css({
                           width: $(ev.target).width()
                        });
                    },   
            zIndex: 9999,                      
            revert: true, 
            revertDuration: 0              
        });
        var extEventData = { title: item.title, pid : item.pid, tid: item.tid, duration: item.duration, trName: item.trName, location: item.location, stick: item.stick };
        elem.data('event', extEventData);
    });
}

Unfortunately I am getting this error in google chrome. This code works fine with Fire Fox.

I tried to parse the stored data as var parsedItem = JSON.parse(JSON.stringify(extEvent)) also. But this fires a "Uncaught TypeError: Cannot use 'in' operator to search for '14' in [object Object]" in both chorme as well as firefox.

Johncy Binoy
  • 169
  • 3
  • 13
  • 1
    You're trying to parse something that already is an object. –  Dec 06 '16 at 05:11
  • Possible duplicate of [I keep getting "Uncaught SyntaxError: Unexpected token o"](http://stackoverflow.com/questions/8081701/i-keep-getting-uncaught-syntaxerror-unexpected-token-o) –  Dec 06 '16 at 05:12
  • @ChrisG Thanks for replying. I tried parsedItem = extEvent also. But it fires "jquery.js:4 Uncaught TypeError: Cannot use 'in' operator to search for '14' in [object Object]" in chrome and "TypeError: invalid 'in' operand e" in firefox. Also [I keep getting “Uncaught SyntaxError: Unexpected token o”](http://stackoverflow.com/questions/8081701/i-keep-getting-uncaught-syntaxerror-unexpected-token-o) it speeks about using ajax calls. But in my case there is no ajax call. It just get the local storage data and iterates through each item. – Johncy Binoy Dec 06 '16 at 05:40
  • It all comes down to what exactly `extEvent` is after you've checked it out of localStorage. What does the console show? A string or an object? The possible duplicate is not just about an AJAX call but mostly about the fact that the response of said call is already an object, as opposed to a JSON string. –  Dec 06 '16 at 15:29

0 Answers0