2015-02-24 122 views
0

我正在嘗試使用JQuery來讀取和解析文本文件,並且我正在使用的代碼似乎出錯了。使用javascript讀取文件

//Attempt 6 
alert("Test Alert 9"); //js file does load into index.html 
$.get("exchanges.txt", 
    function(data) { 
     //idk what the following two lines do, I got them from: 
     //http://api.jquery.com/jquery.get/ 
    $(".result").html(data); 
    alert("check it"); 
    }) 
    //should execute if works? 
    .done(function() { 
     alert("second success"); 
    }) 
    //should execute if any error 
    .fail(function() { 
     alert("error"); 
    }) 
    .always(function() { 
     alert("finished"); 
    }); 

以下提示打印:

  1. 「檢測警報9」
  2. 「錯誤」
  3. 「已完成」

我的問題: 是有辦法檢查什麼的錯誤是? 和/或沒有人知道錯誤可能是什麼?

編輯:爲了澄清,exchanges.txt文件位於同一文件夾中的js文件

編輯:更新拋出錯誤控制檯。錯誤打印:

XMLHttpRequest cannot load  file:///C:/Users/Invictus/Documents/GitHub/BTCExchangesMaterialize/exchanges.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. 

通過將exchanges.txt移動到主文件夾而不是js /文件夾來修復此錯誤。

新的錯誤:

XMLHttpRequest cannot load 
file:///C:/Users/Invictus/Documents/GitHub/BTCExchangesMaterialize/exchanges.txt. 
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.jquery-2.1.1.min.js:4 n.ajaxTransport.k.cors.a.crossDomain.sendjquery-2.1.1.min.js:4 n.extend.ajaxjquery-2.1.1.min.js:4 n.each.n.(anonymous function)exchangesParser.js:3 (anonymous function) 
+0

檢查瀏覽器的網絡選項卡(開發人員工具)看看什麼是休息..也失敗處理程序有一個狀態參數 – 2015-02-24 03:57:02

+0

其中是** exchanges.txt **? – 2015-02-24 03:59:35

+0

exchanges.txt文件位於與js文件 – 2015-02-24 04:00:39

回答

0

$.get是$就與一些設置的shorcut。 簽名的$不用彷徨是:

$.get([String] url, [Object|String] urlparams, [Closure] callback);

callback is excecuted AFTER AJAX QUERY HAD SUCCESS.

如果您打算使用jQuery承諾的方式,省略了成功回調,並使用其承諾的方法,.done().fail().always()

成功回撥方式

$.get("exchanges.txt", function(data) { 
    $(".result").html(data); 
    alert("checked it"); 
}); 

承諾方式

$.get("exchanges.txt") 
.done(function(data) { 
    $(".result").html(data); 
    alert("checked it"); 
}) 
.fail(function() { 
    alert("error"); 
}) 
.always(function() { 
    alert("finished"); 
}); 

可以看出,承諾方式及其對處理更具體的,因爲你可以調用一個函數的詳細點說得到,這是因爲$.get(正如我前面提到)是shorcut for $.ajax。看一下$.get電話擴展爲$.ajax

$。AJAX方式

$.ajax({ 
    url: "exchances.txt", 
    success: function(data){ 
     $(".result").html(data); 
     alert("checked it"); 
    }, 
    error: function(response){ 
     alert("error"); 
    }, 
    complete: function(response){ 
     alert("done"); 
    }, 
}); 

只要記住,Ajax請求不能做本地文件與file://引用它時,你應該通過HTTP(Apache的,節點)訪問它們,FTP或任何其他協議ü希望,但從未下file://

瞭解更多:

jQuery promises
jQuery ajax

+0

'相同的文件夾啊,是嗎使用file://和http訪問文件的一種方法?或者我將不得不安裝並運行本地服務器以便能夠使用文件抓取? – 2015-02-24 04:15:21

+0

由於安全原因禁止本地ajax查詢,因此最好設置本地服務器。 – josegomezr 2015-02-24 04:16:31

+0

謝謝!我會嘗試。如果我看起來有點天真,那是因爲這是我的第一個網站開發項目。 – 2015-02-24 04:19:59

0

fail功能,通過response。然後你可以玩響應對象。啓用前檢查瀏覽器的控制檯(開發工具)

.fail(function(response) { 
     console.log("my error response is",response); 
     alert("error"); 
    })