How to read local xml file using javascript? - javascript

Maybe the problem is that i wrote path to my file incorrectly, i'm using Linux, do i have to write path in some different way?
<script type="text/javascript">
function func(){
var xmlFile = new XMLHttpRequest();
xmlFile.open("GET", "/home/kat/course/data.xml", false);
xmlFile.send();
xmlDoc = xmlFile.responseXML;
document.getElementById("result_field").value = xmlDoc;
}
</script>

The file needs to be in the webroot.
So if you have the code in /var/www and your site is 'somedomain.com/index.html' you will need to have the xml file in the same /var/www directory and access it like "GET", "somedomain.com/anotherfolder/data.xml"...
You can't access server directories from JS... The javascript runs on the client side, not on the server side, so the file needs to be accessible on the website.

JavaScript can only access files your server is serving. Unless you are serving /home/kat/course/data.xml, you're going to need to move data.xml before your JavaScript can access it. Once you move it to a location being served, it will be accessible via an XHR.
As a side note, the responseXML property of an XHR is a Document object, not a string. See: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseXML

There could be two problems. The url must be {your_server_domain}/home/kat/course/data.xml, and the document.getElementById("result_field") must be an input/textarea to accept value attribution, otherwise, use innerHTML.
P.s.: Your local server domain usually is: localhost or 127.0.0.1:80.

Related

How to find html files running from server or not using javascript?

I'm new to javascript might be this question looks silly. How to check whether the html files are loading from local file system or server using javascript?
For example when I open the html files from my local system (/home/user/1.html) browser shows in url
file:///home/user/1.html
But if i load 1.html file into my local server then, if i access that file browser shows in url like below
http://localhost/GUI/1.html
I want to find whether files are loading from my server or local file system using java script.
Is there any way to find this using java script method.
You can use the window.location object to do that.
use window.location.protocol property.
The following line of code will return true if the file is being served from the file system.
window.location.protocol == "file:";
It basically checks if the protocol being used is file or not.

xmlhttprequest javascript - where do i place my json file

Ive got this code in my script tag at my index file
var xhr = new XMLHttpRequest();
xhr.open("GET","images.json",true);
xhr.onload = function () {
parsedJSON = JSON.parse(this.responseTEXT);
console.log(parsedJSON["frames"]["turn.png"]["rotated"]);
};
Ive executed it through my localhost on node.js
however it keeps coming up with 404 not found.
now i dont now where to put the file at.
in developer tools it shows that its been seached for at http://localhost:2000/images.json
i do not now where or how to place it in the local host, can anyone help
sorry for broken english.
If you're using Express...
Try creating a folder called public in the same folder as the file you execute with Node. Then, just pass that folder name to the express.static middleware like this:
app.use(express.static('public'));
If you put images.json into the public folder now, express.static will find it and send it to your browser when the file is requested by your script.
And, as epascarello pointed out, JavaScript is case-sensitive. You'll want to change responseTEXT to responseText.

How to read and store XML generated by CGI script from a URL address

I have a problem about reading XML file which generated by CGI
Say I have a url address like this:
http://test.mywebsite.com/cgi-bin/generateXML.cgi?listxml=1&oid=10037&bidderid=6217&aid=25731&tote=
If I open it in the browser, it will return a xml file and display it in the browser.
Now what I need to do is using javascript and jQuery to retrive this XML file generated by this address.
I have try the code like:
$(document).ready(function()
{
alert("HELLO!");
var self = this;
var target;
$.get("http://test.mywebsite.com/cgi-bin/generateXML.cgi?listxml=1&oid=10037&bidderid=6217&aid=25731&tote=", function(d){
$(d).find("sale").each(function(){
target = ($(this).find("target").text());
alert(target);
});
});
});
But this doesn't work. The XML can't be get.
Also I need to mention that the code is now on my local computer, and the URL address, the cgi file is on another server. So I guess there may be some cross domain restriction?
I just have no idea where to start with. Can anyone help me? Thank you!!
Probably the better way is using jQuery.parseXML()
http://api.jquery.com/jQuery.parseXML/
So I guess there may be some cross domain restriction?
Yes. If you have control over the other server you'll have to set some CORS headers to the cgi response, like
Access-Control-Allow-Origin: localhost

Javascript Read Text File Offline

Currently I use this code to read a txt file with words and perform some operations. However, this particular code requires the html to be deployed on a server. Is there any workaround where I can replace this code with something else to read the file without the need of a server?
var xhr = new XMLHttpRequest();
xhr.open( "GET", "dictionary.txt", false );
xhr.send( null );
var words= xhr.responseText.split(",");
It is NOT possible to call Ajax outside your server domain (except you use scriptagproxy, that too requires you to have some server side configuration). So, in short, you CANNOT read files on your local computer using Ajax calls.
You might like this article.
The file selection can be made either by input or drag-and-drop (not otherwise). Please see: this
You cannot read files from the clients' computer, so the text file you are reading must be on the same server as your javascript.
However, if you are loading the HTML file from your computer (e.g. file://c:/../test.html), you might be able to read files located on ONLY your computer by using relative paths.
You can hide an iframe on the page, with its src='dictionary.txt',
and read or manipulate the iframe's local content when it's onload event fires.

How to read contents of a file using javascript?

I have an input type="file" button. After I choose a file, I have to read the contents of the file using javascript. Is it possible to read/get contents of a chosen file using javascript or ajax?
You are all wrong in a way. It is possible. With the new File API you can read files before submitting them to the server. It is not available in all browsers yet though.
Check this example. Try to open a text file for example.
http://development.zeta-two.com/stable/file-api/file.html
Edit: Even though the question states "uploaded file" I interpret it as, "a file to be uploaded". Otherwise it doesn't make sense at all.
With AJAX its possible to read uploaded file but with pure javascript its not possible because javascript works on client side not on sever side.
if you are going to use jquery than Ajax call may be like this
$.ajax({
url: "test.html",
context: document.body,
success: function(){
$(this).addClass("done");
}
});
Reading files client side is hard:
How to read and write into file using JavaScript
Read a local file
Local file access with javascript
Unless you are trying to do it with local javascript:
Access Local Files with Local Javascript
Or server side javascript:
http://en.wikipedia.org/wiki/Server-side_JavaScript
Alternatively you can force your user to install an ActiveX object:
http://4umi.com/web/javascript/fileread.php
you cant do it using javascript directly. You can post the file to the server an then use ajax to retrieve the content.
Javascript is designed not to have access to the computer it is running on. This is so that rogue javascript can't read the user's harddrive.
You could look into using iframes though.
It is not possible to do it in java script. See Local file access with javascript
I agree with DoXicK above. You can post the file first on server and then you can use Ajax to read it.
That is not entirely impossible
A browser's usually runs Javascript(JavaScript Engine) in a sandboxed environment.
So you can use Windows Scripting Host or Internet Explorer in a trusted environment and use the FileSystemObject
or use
Or upload a file to your server and use the XMLHttpRequest object.(in other words - Ajax)
For IE use the FileSystemObject (which is found on all Windows systems).
For Firefox:
var file = Components.classes["#mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("/home");
See https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO
To see these methods and others in use, look at TiddlyWiki app to see how it does it across all major browsers.

Categories

Resources