2013-09-21 66 views
2

在我的節點js文件,我有這樣的代碼:獲取協議

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文件。但是,當我訪問該頁面時,出現錯誤:

Request Failed: error, Protocol not supported. 

有誰知道最新錯了嗎?

謝謝。

+0

在服務器上我們讀取文件不是getJSON getJSON用於前端 – Gaurav

回答

1

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

如果文件位於服務器上,爲什麼不用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

顯然$.getJSON使用一個意外的(可能爲null)值作爲該協議沒有規定當它。