-1

I'm working on a project on my computer and I'm getting "not well-formed" error when trying to read a json object from a file located on my machine.

It happens on Firefox. In Chrome, I'm getting "XMLHttpRequest cannot load file...".

I've tried changing the Mime-type to application/json but in vain.

Could you please help?


My code :

$.ajax({
    url: 'json/local_json_file.json',
    dataType: 'json',  //or application/json
    type: 'get',
    cache:'false',
    success: function(data) {
        $(data).each(function(index,value) {
            //some code
        });
    }
});
Community
  • 1
  • 1
Beppe
  • 189
  • 2
  • 4
  • 14

1 Answers1

0

if this is a local file, you should be using $.getJSON It is still asynchronous.

$.getJSON("json/local_json_file.json", function(json) {
     //do stuff to the json object here
});
Pabs123
  • 3,385
  • 13
  • 29
  • 1
    `$.getJSON` is just a shortcut for `$.ajax` with `dataType: 'json'`. They both end up in the same jQuery code to perform the AJAX request. – Barmar Feb 12 '16 at 21:55
  • I've used it but got the same problem. – Beppe Feb 12 '16 at 21:56
  • alright makes sense! I'll leave the answer up for the sake of future readers, your comment is helpful – Pabs123 Feb 12 '16 at 21:56