Home > Blockchain >  json parse error in ajax return with an html comment
json parse error in ajax return with an html comment

Time:02-11

The attached image shows what is coming back from an ajax call to a PHP script. The script builds the simple array that you see, and for some reason it adds the html comment before the array.

enter image description here

<!-- translation function -->

No idea where this is coming from, or what it is for. I'm just wondering what this is and if anyone has ever seen it before. It is causing a json parse error, unexpected token <

Thanks!

CodePudding user response:

I don't know what it is and searched for it on Google, found no sensible results. So it is probably something related to your project or some tool that it uses. While you do not know the exact source yet, you can remove the comment of a JSON using a function like this:

function stripHTMLComments(json) {
    let parts = json.split('<!--');
    for (let index = 0; index < parts.length; index  ) {
        if (parts[index].indexOf('-->') >= 0) parts[index] = parts[index].substring(parts[index].indexOf('-->')   3)
    }
    return parts.join("");
}

console.log(stripHTMLComments("<!-- foobar -->{'a': 1, 'b': 2}"));

  • Related