Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 746
omega Avatar asked Apr 18 '26 01:04

omega


1 Answers

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.

like image 53
fardjad Avatar answered Apr 19 '26 15:04

fardjad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!