2
$(document).ready(function() {
    $.getJSON('http://192.168.99.68/test.json?callback=?', function(json) {    
        alert("abc");
    });
});

It doesn't work. There is a JSON file on linux VM with 192.168.99.68. When I run http://192.168.99.68/test.json?callback=? on browser, it returns true JSON.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38

1 Answers1

2

If there is a callback function in this json file, it means that it's created dynamically, right? So, if it is in php and you are the owner of the json file, you can use this header:

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

You can replace * with the domain, that will access to this json file. * means, that all domains are allowed to access by JavaScript.

* OR *

If you aren't the owner or you don't want to edit the headers of the json file, you can use the callback function, that the json file seems to support:

<script type="text/javascript">
function getJSON(json) {
    alert("Got JSON!");
    // do something with json
}
</script>
<script type="text/javascript" src="http://192.168.99.68/test.json?callback=getJSON" async="true"></script>
DragonWork
  • 2,415
  • 1
  • 18
  • 20
  • thank DragonWork. A true url i want to get json data is 192.168.99.68:8096/client/api?.... this url return true json on browser. But when i use your script, it error:invalid label. If I use the file json test.json copied json text from web ), it return nothing and no alert. – user1222893 Feb 21 '12 at 09:35
  • The content of "http://192.168.99.68/test.json?callback=getJSON" should look like `getJSON({i:"am",a:"json object"})`, but it seems to deny a custom callback. So, you can only try the header-method above, if you aren't the owner, you can't do anything. It is a browser-based protection. – DragonWork Feb 21 '12 at 10:03