1

getJson j-query method is not working in chrome while it is working in all other browsers

following is my code

$(document).ready(function(){

$.getJSON('aptitude14.json',function(data){


     /// my code goes here

}; };

error i am getting in chrome is

enter image description here

my JSON file is with in the same folder

4
  • You're hitting the 'same origin' security policy. Chrome is pretty strict about local resources too (they're not considered 'of the same origin'). Commented Feb 2, 2014 at 6:41
  • what i have to do Commented Feb 2, 2014 at 6:48
  • 1
    you have to not use the filesystem. Install a webserver. Commented Feb 2, 2014 at 6:48
  • wow its working thq for your answer Kevin B ,, sje397 Commented Feb 2, 2014 at 6:52

1 Answer 1

1

You are trying to read a file from your client machine with file: protocol, which is not safe. It doesn't work in Chrome, because Chrome cares about the security issues more than other browsers. it seems to me you are trying to create a sample ajax.

What you can do is simply disable the web security in chrome, if it is just a sample testing, open your Chrome in comand line with this parameter: --disable-web-security

and if it is not a sample test, you have to put your file on a server that you have access to it and use http ajax call instead of file ajax call.

If you had this problem on the web, there would be two options to fix it, using JSONP or enabling CORS. but in file protocol I can offer you to fake a JSONP call like this:

-first it you json file is like this:

[{
   name : "abc" //...
},
{
   name : "efg" //...
}]

change it to (wrap it in a function call):

callback([{
   name : "abc" //...
},
{
   name : "efg" //...
}])

then do this instead of jQuery ajax call:

window.callback = function(jsonObj){
    //
};
var script = document.createElement('script');
//since this is a fake jsonp call you don't need to add ?callback=callback to url
script.src = 'aptitude14.json';
document.getElementsByTagName('head')[0].appendChild(script);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.