I developed a module for Magento 2.4 that consumes an API with multiple endpoints and one of them is returning the response as JSON.

If I create an AJAX call, the response I will get on the desktop version is a valid JSON, but on mobile, the same AJAX call gives an error (SyntaxError: JSON Parse error: Unrecognized token '<').

If I remove "dataType: JSON" and parse the returned AJAX response as a string on mobile, I will see that it contains a short tag (script ... pub/errors/report.php ... ).

What could be the issue behind this? I can't parse the response on mobile as JSON, but everything works as expected on the desktop version.

Thank you!

  • script ... pub/errors/report.php -> Which means your script throws error and doesn't return the JSON
    – Sohel Rana
    Nov 19, 2022 at 4:47
  • According with the API Docs, that endpoint should return a JSON and indeed, it is a JSON. If I pass “dataType: json”, I will see a valid JSON response on desktop, but not on mobile where an error is given. A short-term solution was to remove the dataType line, split the response (which is now available as a string) by “<script” and then parse the first part as a JSON object. That would work both on mobile and desktop, but as I said, it’s strange and I am sure I can find a better solution or the reason behind it.
    – iupaem
    Nov 19, 2022 at 6:21


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.