2015-02-24 120 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"); 
    })