This is the javascript I'm using to make an AJAX call to a PHP:
$(document).ready(function(e) {
$(function(){
$.ajax({
type:'GET',
dataType: 'jsonp',
data: {
country: "uk"
},
url: 'http://api.mysite.uk/advertorial/index.php',
success: function (response){
var result = $.parseJSON(response);
console.log(result);
},
error : function () {
console.log('Error');
}
});
});
});
which returns a JSON structured like this:
{"id":"1","name":"test","country":"uk","header":"Header","pre_cta_text":"Pre CTA","cta_text":"CTA text","cta":"CTA","img":null,"active":"1"}
Even if the call gives an error, I can see that it's returning the JSON above. I particular the error I get in browser console is:
Uncaught SyntaxError: Unexpected token : index.php?callback=jQuery32103297264247416809_1516181997373&country=uk&_=1516181997374:1
You instruct jQuery to ask for JSONP and decode it automatically:
dataType: 'jsonp',
Then you take jQuery's decoded data and handle it as JSON, which is not and has never been:
success: function (response){
var result = $.parseJSON(response);
console.log(result);
}