11

Possible Duplicate:
Problems with jQuery getJSON using local files in Chrome

Both my html file "index.html" and "contact.json" are on same folder

My code to fetch json file

function loadData(fileName) { 
    // getting json from a remote file
    // by returning the jqXHR object we can use the .done() function on it
    // so the callback gets executed as soon as the request returns successfully
    return $.getJSON( fileName + ".json");
}

function fillDataTable(data) {
//  alert(data);
    // iterate over each entry in the data.info array
    $.each(data.info, function(index, element) {
    // append it to the table
    $("#contact").append('<tr><td>' + element.name + '</td><td>'+ element.email +'</td><td>' + element.phone +'</td><td>' + '<input type="button" id="edit" onclick="edit(this.name)" name="'+ element.name +'" value="Edit"></input>' +'</td></tr>')
    }); 
}

$(document).ready(function(){

    // the file's name. "contact" in this example.
    var myFile = "contact";

    loadData(myFile).done(function(data) {
        // check if we acutally get something back and if the data has the info property
        if (data && data.info) {
            // now fill the data table with our data
            fillDataTable(data)
        }
    });
});

My json file

{
    "info": [
        {
            "name":"Noob Here",
            "email":"myemail@server.com",
            "phone":"123456"
        },
        {
            "name":"Newbie There",
            "email":"hisemail@server.com",
            "phone":"589433"
        }
    ]
}

My html

<div id="container-1">
    <ul>
        <li><a href="#fragment-1">List</a></li>
    </ul>   
    <div id="fragment-1">
        <table id="contact">
        <tr>
        <th> Name </th>
        <th>E-mail </th>
        <th> Phone </th>
        </tr>
        </table>
    </div>
</div>

CDN give

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js"></script>

The code works fine on Firefox but not working in Google Chrome.

Error obtained in Google Chrome

XMLHttpRequest cannot load file:///home/user/jquery/contact.json. Origin null is not allowed by Access-Control-Allow-Origin.

Both html and json file are in same location. How to fix it?

Community
  • 1
  • 1
Okky
  • 10,338
  • 15
  • 75
  • 122
  • Chrome has pretty decent developer tools (hit `F12` to open them). You should at least be able to see possible error messages and find out which code is executed and which not. – Álvaro González Feb 04 '13 at 13:02
  • updated question with the error code – Okky Feb 04 '13 at 13:03
  • @h2ooooooo: I didn't find that it was problem with getJson or Chrome till i posted this question. So i believe its not a duplicate. – Okky Feb 04 '13 at 13:53

2 Answers2

9

Chrome simply doesn't allow ajax calls to be made to local files. Refer This

You can however launch Chrome with the flag --allow-file-access-from-files if you want it to get it working locally.

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
Peter Herdenborg
  • 5,736
  • 1
  • 20
  • 21
3

Check the answer here

try to access json file via http or use jsonp

Community
  • 1
  • 1
marbor3
  • 439
  • 3
  • 13