How to read local xml file using $.ajax? - javascript

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. 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.
To read a local file in a Firefox addon you can use Components (file IO) like so:
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");

Related

create document element from string

I'm trying to use Mozilla Readability stand-alone library for a personal project I am currently developing. The idea is to pass Mozilla Readability a document element and that after some parsing magic it returns the document's title, author, text, etc.
So, the first thing is dealing with how to get the HTML source of an external URL. I have dealt with that using an internal PHP file, which retrieves this external URL's source code.
After that, I call an AJAX GET to process the data returned by my PHP file. However, I am having lots of problems to convert this HTML source into an actual Javascript document element to pass to Mozilla Readability.
This is the code that I am currently using:
$('#btn_fetch').on('click', function() {
var url = 'https://www.rtl.be/info/monde/france/pierre-bellemare-s-est-eteint-a-88-ans-1025563.aspx';
$.ajax({
type: "GET",
url: 'fetchurl.php',
data: {
url: url
},
dataType: "html"
})
.done(function(data) {
var doc = document.implementation.createHTMLDocument("New Document");
// i don't know how to add "data" into "doc" element !
var article = new Readability(doc).parse();
alert(article.title);
})
.fail(function(xhr, ajaxOptions, thrownError) {
alert('Error:' . thrownError);
});
});
I just discovered it was actually very easy. The line I was missing was...
doc.body.parentElement.innerHTML = data;
it should be added here in the .done function
.done(function(data) {
var doc = document.implementation.createHTMLDocument("New Document");
// you need to add it here
doc.body.parentElement.innerHTML = data;
var article = new Readability(doc).parse();
alert(article.title);
})

load external xml file into js variable to be using jquery

I am trying to load an xml file containing config for a single page apps logic using jquery.
I can get the console to show the xml has been loaded and even display the xml in console but have yet to be able to get the xml to be declared as a string variable. I am using the following code
$.ajax({
type: "GET",
url: "dummy.xml",
dataType: "xml",
success: function (url) {
console.log('started function xml');
console.log(url);
// Parse the xml file and get data
parseXMLConfig;
}
});
I have a separate function to parse the xml using jquery which works if I declare the xml directly in the JavaScript but need to separate the xml and js for later development efforts.
Can anyone tell me how to set the js object to a variable for use elsewhere in scripts.
You can do something like this
$.ajax({
type: "GET",
url: "dummy.xml",
success: function (xmContent) {
console.log(xmContent);
xmlDoc = $.parseXML(xmContent),
$xml = $( xmlDoc ),
$title = $xml.find( "title" );
console.log($title );
}
});
For more detail refer DOC

ajax parse xml response function wont recognize XML file URL

im using the following code from another solution on here
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
dataType: "xml",
url: "xmlfile.xml",
success: function(xml){
$("#output").append($(xml).find("address"));
}
});
});
</script>`
and if i use the local xmlfile.xml file it works fine, but when i try to run the same function with the xml file hosted elsewhere, it stops working:
http://www.zillow.com/webservice/GetUpdatedPropertyDetails.htm?zws-id=X1-ZWz1ebcem1ra4r_10irx&zpid=48749425
I tried encoding the URL using escape() and encodeURIComponent()
neither of those worked.
what am i doing wrong? why does the function work with a local xml file but breaks when i use the other URL?

YQL functionality with Javascript implementation

I am using this Ajax call to grab a CSV file from the Internet. Currently the way that it is implemented is with a call to yahooapis and a SQL like query of the file.
function getData(qry){
$.ajax({
url: 'http://query.yahooapis.com/v1/public/yql',
data: {
q: qry,
format: 'json',
_maxage: 120,
ts: getTS(dateNow)
},
cache:true,
dataType: 'jsonp',
success:function(data){
setFlowData(data);
}
});
}
The qry is similar to something like this:
select * from csv where url IN ('http://example.com/public/zone.csv')
The file is then parsed by a function similar to this:
if(data.query.count > 0){
var queryData = data.query.results.row;
if (queryData.col2 == rt.id) {
rt.lbmp = queryData.col3;
rt.ts = queryData.col0;
}
}
My question is how do I achieve the same functionality without the use of the YQL/SQL call? Is this possible with JavaScript implementation? Thanks for the help.
Absolutely you can process CSV data without any YQL stuff. You can use $.ajax to pull csv data if it is from your own domain or CORS-ready, or through CORS proxy server. There is also jquery-csv(https://code.google.com/p/jquery-csv/) or PapaParse(http://papaparse.com/) to parse CSV data.

Javascript Blob changes file

I really have no prior knowledge to Blob objects in JavaScript aside from what I've read here but need to use them to convert a string of binary data into a .xls file which I then make available to the user. The catch is when I construct a blob, get the location, and open up the file to look at it, it opens up saying
The file you are trying to open is in a different format that
specified by the file extension. Verify that the file is not
corrupted and is from a trusted source before opening the file.
(I know this data is incorrect because when I submit the form normally I get the file correctly and don't have this issue)
This is done in an ajax call and the success functions parameter data is the binary data.
$("#fileForm").submit(function(){
var fileData = $("#fileInputElmt").prop("files")[0];
var data = new FormData();
data.append("upload",fileData);
var url = "process.action?" + $("#fileForm").serialize();
$.ajax({
type: "POST",
url:url,
data:data,
cache:false,
contentType:false,
processData:false,
success:function(data){
var bb = new Blob([data],
{ type: 'application/vnd.ms-excel',endings:'native'});
var bUrl = URL.createObjectURL(bb);
window.open(bUrl,"_self");
hideProgressBar();
},error:function(data){
hideProgressBar();
}
});
return false;
});
Am I doing something wrong? or is there a better way of doing this?

Categories

Resources