Getting protocol not supported when using getJSON on Node js on static json file


In my node js file, I have this code:

var jqxhr = $.getJSON( "favs.json", function() {
    console.log( "success" );
})
.done(function() {
    console.log( "second success" );
})
.fail(function( jqxhr, textStatus, error ) {
    var err = textStatus + ", " + error;
    console.log( "Request Failed: " + err );
})
.always(function() {
    console.log( "complete" );
});

And in the server there is a file called favs.json in the same directory as the above js file. However when I visit the page, I get the error:

Request Failed: error, Protocol not supported.

Does anyone know whats wrong?

Thanks.

And in the server there is a file called favs.json in the same directory as the above js file.

If the file is located on server, why don't you just read it with fs.readFile()?

var fs = require('fs');
var fileContents;
fs.readFile('./favs.json', function (err, data) {
    if (err) throw err;
    fileContents = data;
    // ...
});

If you really want to get the contents of that file using XMLHttpRequest,

  1. Make sure it is accessible via an HTTP(S) server in your application.
  2. Enter the full URL to the file you want to fetch (ex. http://localhost/favs.json.)

Apparently $.getJSON uses an unexpected (possibly null) value as the protocol when it's not specified.