-2

Json hosted online and should be accessed by a link:

   {
response: "yes"
}

Getting Json on a device:

 <script
  src="https://code.jquery.com/jquery-3.1.1.min.js"
  integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
  crossorigin="anonymous"></script> 
 <script>  $(document).ready(function () {
                      var url = 'link is here';
                      $.getJSON(url, function (data) {
                                alert(data.response);

                                });
                      });</script>

Shows nothing. Please advice.

J. Doe
  • 254
  • 1
  • 3
  • 13
  • Have you added something like this on your php page? $data = /** whatever you're serializing **/; header('Content-Type: application/json'); echo json_encode($data); – Mart Oct 30 '16 at 21:42
  • @MartHaarman On html page – J. Doe Oct 30 '16 at 21:42
  • what does `console.log(data)` output? – Searching Oct 30 '16 at 21:43
  • @Searching it is a phone gap app which opens with Xcode. When I open it locally with browser the app does not runs, therefore I cant test it with browser. – J. Doe Oct 30 '16 at 21:44
  • Sorry my internet connection went all crazy before i finished my post. I meant to ask if you have something like this: "$data = /** whatever you're serializing **/; header('Content-Type: application/json'); echo json_encode($data);" on the php page you're referring to.. And like you say you use a html page, could you show some more code? – Mart Oct 30 '16 at 21:44
  • Yeap, thats exactly what my php page contains. Regarding other code, there is only cdn of jquery and thats it. – J. Doe Oct 30 '16 at 21:46
  • When you open the link you're referring to in your browser, then it does show data? – Mart Oct 30 '16 at 21:53
  • sorry format mistake try it again https://jsonplaceholder.typicode.com/posts/1 – Searching Oct 30 '16 at 21:54
  • @Searching I pasted the link and it did worked...means problem is with php side. Php: `$alldata = array('response'=>'yes'); header('Content-Type: application/json; charset=utf-8'); echo json_encode($alldata); ` – J. Doe Oct 30 '16 at 21:55
  • You could add this to the top of your page: header('Access-Control-Allow-Origin: *'); This is not rather safe however first it 'll do for testing – Mart Oct 30 '16 at 21:56
  • @MartHaarman worked now. thanks a lot. Write it as an answer and I will mark it. – J. Doe Oct 30 '16 at 21:58

1 Answers1

1

It has to do with the fact that the php needs to grant access. For testing purposes you can add this line on top of your php code:

header('Access-Control-Allow-Origin: *'); 

However this is not safe at all. Therefore you need to write it somehow like this:

header('Access-Control-Allow-Origin: http://mysite1.com');
header('Access-Control-Allow-Origin: http://example.com');
header('Access-Control-Allow-Origin: https://www.mysite2.com')  

This is all mentioned and explained in here: how to bypass Access-Control-Allow-Origin?

Community
  • 1
  • 1
Mart
  • 475
  • 4
  • 21