1

ovar . tk/server.php:

[{"nama":"cilok","tempat":"Jajanan cilok dapat di temukan di daerah sekitar gerbang     FAPET","gambar":"cilok.jpg"},{"nama":"martabak","tempat":"Jajanan martabak dapat di temukan di daerah pasar Dinoyo lama","gambar":"martabak.jpg"},{"nama":"cwie mi","tempat":"Jajanan cwie mi dapat di temukan di daerah jalan sumbersari gang 3 ","gambar":"martabak.jpg"},{"nama":"mcd","tempat":"Restoran Mc Donalds dapat di temukan di daerah pasar Dinoyo lama","gambar":"martabak.jpg"},{"nama":"mcd","tempat":"Tidak Ditemukan","gambar":""}]

and my code is test.html:

   <!DOCTYPE html>
<html>
<head>
 <script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.getJSON("http://ovar.tk/server.php",function(result){
      $.each(result, function(i, field){
        $("div").append(field.nama + " ");
      });
    });
  });
});
</script>
</head>
<body>    
<button>Get JSON data</button>
<div></div>

</body>
</html>

nothing happen when i click the button, it show the data when the url is localhost im newbie in getting json data, so please help me! Thank you all! :D

Tim
  • 9,171
  • 33
  • 51
dadan
  • 124
  • 2
  • 11

2 Answers2

0

It looks as if you may be falling foul of the "same origin policy" which stops JavaScript in regular pages loading data from servers other than the one which the page came from.

Tim
  • 9,171
  • 33
  • 51
0

I guess you're having a cross-origin resource sharing problem, take a look at enable-cors.org. You should be using JSONP to solve the problem by adding

 dataType: 'jsonp'

to your request.

Something like:

$.ajax({
     url:"http://ovar.tk/server.php",
     dataType: 'jsonp', 
     success:function(json){
         // do stuff with json 
         alert("Success");
     },
     error:function(){
         alert("Error");
     },
});
Atropo
  • 12,231
  • 6
  • 49
  • 62