Cross domain Json parse to display in HTML using J

2019-08-31 00:52发布

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>");
        });
    });

1条回答
祖国的老花朵
2楼-- · 2019-08-31 01:40

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 ).

查看更多
登录 后发表回答