0

This is my first time working with JSON and I need to display this JSON http://www.rockfm.mx/jsonApps/jsonAlAire.php on a webpage that is not on the same server as the JSON I know I have to use JSONP but my code is not working at all here it is an example

<script>
$(document).ready(function(){
});
$("#loaduserdata").click(function(){
    $("#userdata tbody").html("");
    $.getJSON(
        "www.rockfm.mx/jsonApps/jsonAlAire.php",
        function(data){
            $.each(data.userdata, function(i,user){
                var tblRow =
                    "<tr>"
                    +"<td>"+user.tituloDelShow+"</td>"
                    +"<td>"+user.locutores+"</td>"
                    +"</tr>"
                $(tblRow).appendTo("#userdata tbody");
            });
        }
    );
});
</script>
5
  • What errors are you getting? Commented Mar 31, 2014 at 22:13
  • 1
    Have you tried with correct URL - one with http(s):// in front? Commented Mar 31, 2014 at 22:13
  • 2
    "i know i have to use jsonp" The service doesn't seem to support JSONP (or CORS), unless they use different URL arguments. You should read it's documentation. If it doesn't support JSONP, you can't make an Ajax call to it. Commented Mar 31, 2014 at 22:15
  • ^^^ I'm getting same-origin errors, no Access-Control-Allow-Origin header, and JSONP doesn't seem to be supported. Commented Mar 31, 2014 at 22:16
  • You probably need to write a script in php on your own server using something like cURL to get the json and make an ajax call to that instead. Commented Mar 31, 2014 at 22:18

2 Answers 2

1

I think that the problem is the same origin security policy. My opinion is that the only solution is to use a php script on your own server that act as a gateway between your ajax and the remote server. How write the code depends on some factors related to your server configuration. A really simple solution (if your server support allow_url_fopen) can be something like:

getServerData.php

<?php
 echo   file_get_contents('http://www.rockfm.mx/jsonApps/jsonAlAire.php');
?>

Then, call getServeData.php with your script. With this little trick you will bypass same-origin restriction.

hope it help

Sign up to request clarification or add additional context in comments.

Comments

0

You forgot http://

<script>
$(document).ready(function(){
});
$("#loaduserdata").click(function(){
    $("#userdata tbody").html("");
    $.getJSON(
        "http://www.rockfm.mx/jsonApps/jsonAlAire.php",
        function(data){
            $.each(data.userdata, function(i,user){
                var tblRow =
                    "<tr>"
                    +"<td>"+user.tituloDelShow+"</td>"
                    +"<td>"+user.locutores+"</td>"
                    +"</tr>"
                $(tblRow).appendTo("#userdata tbody");
            });
        }
    );
});
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.