[英]How to read local xml file using $.ajax?
I am trying to build a Firefox extension for which I need to read local xml file but does not able to read file using $.ajax
. 我正在尝试构建Firefox扩展,我需要为其读取本地xml文件,但无法使用$.ajax
读取文件。 My code is like below: 我的代码如下:
$.ajax({
type: "GET",
url: "file:///C:/Users/Mitul Gandhi/Desktop/Library_en.xml",
dataType: "xml",
success: function (xml) { }
});
Due to security reasons javascript ajax calls cannot read local file contents. 出于安全原因,javascript ajax调用无法读取本地文件内容。
To read a local file in a Firefox addon you can use Components
(file IO) like so: 要在Firefox插件中读取本地文件,您可以使用Components
(文件IO),如下所示:
function Read(file)
{
var ioService=Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var scriptableStream=Components
.classes["@mozilla.org/scriptableinputstream;1"]
.getService(Components.interfaces.nsIScriptableInputStream);
var channel=ioService.newChannel(file,null,null);
var input=channel.open();
scriptableStream.init(input);
var str=scriptableStream.read(input.available());
scriptableStream.close();
input.close();
return str;
}
var contents = Read("C:/Users/Mitul Gandhi/Desktop/Library_en.xml");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.