Pergunta

Im trying to retrieve some information from a json using the getJSON jquery method from a remote source.
Sample url that is not working:

https://store.sonyentertainmentnetwork.com/store/api/chihiro/00_09_000/container/GR/en/999/EP0001-NPEB00932_00-GASSASSINS000001

When the url above is set as json fails due to cross-origin policy.
So making it jsonp request (adding the &callback=? on the end of url) returns nothing.
I have noticed with jsonp request the request passes(waiting for store.sonyentertainmentnetwork.com on browser displaying) but returns nothing.
Also noticed when jsonp url enter in browser returns this string:

{"codeName":"ResourceDoesNotExist"}

I tried the same script with yahoo json apis and working fine.
Sample url that works:

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20answers.search%20where%20query%3D%22cars%22%20and%20category_id%3D2115500137%20and%20type%3D%22resolved%22&format=json&diagnostics=true&callback=

Code:

$(document).ready(function(){
    $(function() {
        $.getJSON('https://store.sonyentertainmentnetwork.com/store/api/chihiro/00_11_000/container/GR/en/999/EP0001-NPEB00932_00-GASSASSINS000001&callback=?',
        {},
        function (data) {
            $.each( data, function ( i, val ) {
                var age_limit=val['age_limit'];
                $(".age_limit").append(age_limit+'<br>');
            });
        })
    });
}); 
</script>
</head>
<body>
<ul class="age_limit"></ul>
</body>

Working jsfiddle
Not working jsfiddle

I have also tried the abole example as ajax and pure javascript but the results was the same.
Is sonyentertainmentnetwork blocking json as jsonp remote access somehow or what?

Thank you.

Foi útil?

Solução

You are encountering this issue because JSON-P is a feature implemented by the server, or API provider. The sony API you are using does not implement JSON-P, and therefore you cannot simply append &callback=something at the end of your URL to get JSON-P to work.

One workaround for this problem is to have a server backend that makes the request to the API (effectively creating a proxy). When servers make a request in the backend, it does not encounter the Cross Origin issue your browser does. There are services out there that provide this functionality if you don't want to set up your own backend, though there may be fees/limitations.

jsonp.jit.su is one of such services. jsonp.jit.su implements JSON-P, and can make a request to an API that does not support JSON-P. To use it, simply specify your ultimate API endpoint as the url parameter, and specify your callback function as the callback parameter, as follows:

http://jsonp.jit.su/?callback=myCallback&url=https%3A%2F%2Fstore.sonyentertainmentnetwork.com%2Fstore%2Fapi%2Fchihiro%2F00_09_000%2Fcontainer%2FGR%2Fen%2F999%2FEP0001-NPEB00932_00-GASSASSINS000001

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top