Skip to content
Advertisement

PHP/JavaScript apparently not detecting JSON API as valid

I’m trying to make a simple API with PHP and a way to retrieve data from it in JavaScript but I keep getting the following error: SyntaxError: Unexpected token < in JSON at position 0. The JSON is valid and I’m not sure what is causing the error…

I’ve tried setting headers and the data is encoded with json_encode() so there’s not really any excuse for it to not be working.

API link: [removed by author]

PHP Code:

JavaScript

JS Code:

JavaScript

Advertisement

Answer

your response is JSON data. So you should to remove the line of var result = JSON.parse(body.body). Just use body as result.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement