I am facing an issue with parsing a JSON string retrieved from PHP when the page loads. It's strange that if I send an AJAX request to the same function, the parsing is successful without any errors.
The problem arises when I attempt to do this:
JSON.parse('{"products":[{"id":"1","name":"Batata Frita ",... [truncated]
The above JSON string is outputted by PHP in the following way:
function initializeData() {
initialData = JSON.parse('<?php echo Mage::helper('module/endpoint')->getData(); ?>')
...
Despite the fact that the JSON has been validated and tested using a JSON Validator, it still throws this error:
VM3617:1 Uncaught SyntaxError: Unexpected token c in JSON at position 248
at JSON.parse (<anonymous>)
at <anonymous>:1:6
However, if I perform an AJAX call to my server and parse the returned data (which is identical to the previous string):
jQuery.ajax({
type: "POST",
url: '<?php echo Mage::getUrl('module/enpoint/getData') ?>',
data: {id: sellerId, store_id: storeId},
success: function (data) {
data = JSON.parse(data)
...
It works flawlessly without any issues.
Is there something missing in my approach? What could potentially cause this discrepancy in behavior?