0

I did some research on how to parse Json into html with jsonp,because the json is on remote domain. But I can't figure out why is this error and how to handle it:

Uncaught SyntaxError: Unexpected token :

This is my code:

$("document").ready(function() {
        var url = "http://demos.158.bg/football/apis/eplApiV1.php?action=getRoundListByLeagueID&leagueID=7";

        $.getJSON(url + "?callback=?", null, function(jsonp) {

            $("#div-my-table").text("<table>");

            $.each(data, function(i, item) {
                          // doing some stuff here
            });

            $("#div-my-table").append("</table>");
        });
    });

Thanks in advice.

EDIT: Found solution. For those who will come to this post just make php file on your domain with:

<?php
echo file_get_contents($remote_domain_url);

?>

and include that php file url in getJSON. You will no longer need JSONP. Edited code:

$("document").ready(function() { var url = "http://your.domain/phpfile.php";

        $.getJSON(url, null, function(data) {

            $("#div-my-table").text("<table>");

            $.each(data, function(i, item) {
                          // doing some stuff here
            });

            $("#div-my-table").append("</table>");
        });
    });
Hristo Enev
  • 2,421
  • 18
  • 29

1 Answers1

1

You are trying to make a JSONP request, but the server is responding with JSON. You would have to modify the server side code (on demos.158.bg) to support JSONP (i.e. look for a callback argument in the query string and then return an application/javascript document consisting of that value, then (, then the data, then ).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Sorry, but I'm little confused now. Is there a way to do this without touching the server side code? – Hristo Enev Mar 19 '14 at 12:52
  • [This question](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) has pretty comprehensive coverage of the various ways to do cross-domain Ajax. – Quentin Mar 19 '14 at 13:01
  • (But if you want to use JSONP then the server must support JSONP) – Quentin Mar 19 '14 at 13:29
  • Found up my solution. I'll use php file that will echo file_get_contents(remote_domain_url); And that file will be on my domain so i will be able to get the data with getJSON. Thank you for your quick answers anyway :) – Hristo Enev Mar 19 '14 at 14:39