1

I have given a JSON webservice link, under which we have data and wanted to show it on html table using ajax jWuery. If I download it from IE it works locally by using .json format, but webservice link doesn't work. I have checked whether link is working or not but its working in JSON viewer. What to do?

$(document).ready(function() {
    $.ajax({  
       type: "GET",
       data : "{}",
       contentType: "application/json",
       //url: 'myfile.json',
       url: 'http://107.22.160.4/ICatService/Service.svc/GetEvent/10/iphone2x',
       dataType: "json",
       success: function(data) {  
         $.each(data.Get_TappyokaResult, function(index, data){
               var tblRow = "<tr>"+
                             "<td>"+data.Back_id+"</td>"+
                             "<td><img src="+data.Back_image+" class=rowimg /></td>\n"+
                             "<td>"+data.Back_type+"</td>\n"+
                             "<td>"+data.DateModified+"</td>"+
                            "</tr>"
               $(tblRow).appendTo(".dataTable");
               $('table tr:odd').addClass('oddrow');
               $('.dataTable tr:even').addClass('evenrow');
        });
     },  
     error: function(data){  
           alert("error");
     }  
  });
});

myfile.json LOCAL FILE ---------------------------------

{
 "Get_ICatelog_EventResult":[
      {
        "Date":"26\/07\/2012", "Description":"sample event", "Event_Id":5, "Heading":"sample", "Image":"http:\/\/thisisswitch.com\/ICatalogsite\/EventImage\/fc8e84f2-6729-42c0-8e0e-c6961edd2df5.JPG", "Status":0, "Time":"14:00:00", "User_Id":10
      }
  ]
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

Are you using a webserver? if no? make sure u do. file:/// has issues with ajax requests. And if you are, you're probably going to an other origin. (http://107.22.160.4) So it's a cross origin issue.

The origin of the ajax request need to have the same protocol (http/https) domain (domain.com) and port number (defaults to 80) as the target.

There are workarounds like CORS (Cross Origin Recource Sharing) providing the right header information, you will be able to access date on another origin but mostly that requires you to be able to set it on the server side.

Another option is forwarding traffic inside your server.

Or using a php proxy file. That file access the data for you and you can safely access the data or modify headers so you can access them using CORS

Here is php example

You can also use flash proxy or any kind of backend language (asp, jsp, ...)

Community
  • 1
  • 1
VDP
  • 6,340
  • 4
  • 31
  • 53
0

If you have control (or someone you know does) of the web service, you could change the output format to jsonp http://en.wikipedia.org/wiki/JSONP http://api.jquery.com/jQuery.getJSON/

joevallender
  • 4,293
  • 3
  • 27
  • 35