Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Uncaught SyntaxError: "[object Object]" is not valid JSON at JSON.parse (<anonymous>) at myfunction (scriptExtra.js:9:26)

In the title the error i get, this is the code, how can avoid the error, by the way the script works well even with the error

if(window.addEventListener){
window.addEventListener("message", myfunction, false);
}else{
window.attachEvent("onmessage", myfunction);
}

function myfunction(event) {
    if (event.data !== 'null') {
        dataJSONLocator = JSON.parse(event.data);
            if (typeof dataJSONLocator == 'object' && dataJSONLocator.tntMessage !== undefined) {
                if (dataJSONLocator.tntMessage) {
                    console.log(dataJSONLocator);
                    document.getElementsByName( "address1" )[0].value = dataJSONLocator.name + " " + dataJSONLocator.address;
                    document.getElementsByName( "city" )[0].value = dataJSONLocator.city;
                    document.getElementsByName( "id_state" )[0].value = dataJSONLocator.prov;
                    document.getElementsByName( "postcode" )[0].value = dataJSONLocator.cap;
            }   }
    }
}

Just not give the error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In some cases event.data is an Object, but JSON.Parse method expects string value to be able to parse it. That’s how JSON works.

I would look into this line of code if (event.data !== 'null') { and check also if it’s a string.

Something like:

if (event.data !== 'null' && typeof event.data === 'string') {

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading