0

Why getJSON method works only with local files? If I want to take json from local it works, but if I set url with http it doesnt work. why?

<!DOCTYPE html>
<html>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <script>
        $.getJSON("http://www.address.com/getTables.php", function (data) {
            $.each(data, function (i, table) {
                $("#tables").append("<p>" + table.id + " " + table.tabname + "</p>");
            });
        });
    </script>

    <body>
        <div id="tables"></div>
    </body>

</html>

Returned JSON:

[{ "id":"12", "tabname":"cukry" }, { "id":"11", "tabname":"table" }]
Milap
  • 6,915
  • 8
  • 26
  • 46
pkrr
  • 1
  • 1
  • Can you show us your `getTables.php` code? And also what *does* happen when you attempt to execute `$.getJSON`? Maybe use Chrome's developer tools and look at the Network tab or the console. – Eric H Jun 26 '12 at 12:40
  • gettables.php php is correct, it returns Json. If I take a simply Json file by http it doesnt work too (http://www.address.com/test.json). Whats wrong? I only want to show this JSON file – pkrr Jun 26 '12 at 12:44
  • What *does* happen when you attempt to submit the request with `$.getJSON`? Check Chrome's developer tool's console and network tab. – Eric H Jun 26 '12 at 12:45
  • If you are using local files, then you shouldn't be mentioning HTTP address unless the codes are inside a server like WAMP. – SexyBeast Jun 26 '12 at 12:46
  • Is address.com your domain? Does all the code run on this domain? If not, you need JSONP, not JSON. – edwardmp Jun 26 '12 at 12:48
  • It was a problem with different domains, i didnt know that before. thanks a lot edwardmp :) – pkrr Jun 26 '12 at 12:51
  • Perhaps take a look at [this answer](http://stackoverflow.com/a/2686406/586621) related to JSONP – Jay Jun 26 '12 at 13:00

2 Answers2

3

It sounds like you're probably running into the same-origin policy.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • GET http://pkr.mydevil.net/panel/actions/getTables.php 200 OK 154ms jquery-latest.js (wiersz 8240) NagłówkiHTML Nagłówki odpowiedzi Connection keep-alive Content-Type text/html Date Tue, 26 Jun 2012 12:47:31 GMT Server nginx Transfer-Encoding chunked X-Powered-By PHP/5.3.11 Nagłówki zapytania Accept application/json, text/javascript, */*; q=0.01 Accept-Encoding gzip, deflate Accept-Language pl,en-us;q=0.7,en;q=0.3 Connection keep-alive Host pkr.mydevil.net Origin null User-Agent Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1 FirePHP/0.7.1 x-insight activate – pkrr Jun 26 '12 at 12:48
0

Like Matt said it`s because of the same origin policy. Try using JSONP. You just need to add callback to your request URL like this:

$.getJSON("http://www.address.com/getTables.php?jsoncallback=?", function (data) {
        $.each(data, function (i, table) {
            $("#tables").append("<p>" + table.id + " " + table.tabname + "</p>");
        });
    });

See more about JSONP here

Rafael Sedrakyan
  • 2,541
  • 10
  • 34
  • 42