在静态json文件上的节点js上使用getJSON时,不支持获取协议 [英] Getting protocol not supported when using getJSON on Node js on static json file

查看:92
本文介绍了在静态json文件上的节点js上使用getJSON时,不支持获取协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的节点js文件中,我有以下代码:

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" );
});

在服务器中有一个名为 favs.json 与上述js文件位于同一目录中。但是,当我访问该页面时,我收到错误:

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.

有谁知道什么是错的?

谢谢。

推荐答案


在服务器中,在同一目录中有一个名为favs.json的文件如上面的js文件。

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

如果文件位于服务器上,为什么不用 fs.readFile()

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;
    // ...
});

如果你真的想用 XMLHttpRequest获取该文件的内容


  1. 确保可以通过应用程序中的HTTP(S)服务器访问它。

  2. 输入要获取的文件的完整URL(例如 http://localhost/favs.json 。)

  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.)

显然 $。getJSON 在未指定时使用意外(可能为空)值作为协议。

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

这篇关于在静态json文件上的节点js上使用getJSON时,不支持获取协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆