Question

Given the following JavaScrip snippet, I get a JSON parse error. If I hit the URL directly I get a valid JSON response (verified using http://jsonlint.com/).

How to get more information on the parse error?

var purl = 'http://data.police.uk/api/crimes-at-location?date=2012-02&lat=52.629729&lng=-1.131592';
$.ajax({
    type:'GET',
    dataType:'jsonp',
    jsonp:'jsonp',
    url: purl,
    success:function(data){ console.log('success',data)},
    error:function(e, x, y){console.log('error', e, x, y)}
});

The error in the console is :

error Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…} parsererror Error {} 
Était-ce utile?

La solution

It's valid JSON, but it's not valid JSONP as JSONP should be wrapped in a callback function.

That service doesn't support CORS either, so you simply can't get the JSON with ajax, you'll have to use the serverside, and do an ajax call to your own server instead, as the same origin policy prevents you from getting the JSON directly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top