How to fetch a file on a web server using JavaScript? - javascript

I am trying to write a small documentation tool to be used from the browser. It would need to fetch source code files from a web server. What would be the appropriate way to fetch files from JavaScript itself and then read them so they can be parsed ? The file to be fetched is on a different web server.
thanks in advance,
vivekian

Use some sort of ajax framework (or XmlHttpRequest) that would read a file, parse it and display it.
You'll have to create a proxy to that other server. Otherwise you're going to run into security exceptions.
Given your main url http://www.x.com/help.html, and the source files that are located at http://www.x321.com/src/, you're going to create a proxy at http://www.x.com/proxy/ to http://www.x321.com/src/

Related

auto load php script

I have shared folder between in my server which will allow other server to send XML file to me and I want my script read this file auto without opening any page.
I know how to open and read the file.
But the issue how to auto load in the backhand.
you have to create a one page which will read the provided file and do the required actions , then share this URL and format with the team who will going to provide you the xml file.
It is very much like API Endpoint, Where you have to write the code which will handled request and in this scenario your Endpoint will treat as a server and XML file provider will treat as clients.
I hope this answer helps u.
Thanks
Traditionally, you need your server to periodically execute the script which reads the XML. That PHP will need to parse the XML and handle the changes.
Alternatively, the source of the API can use push notification to avoid polling with your server. The XML will be received whenever a change occurred on the server without the creation of a lot of useless requests, but the XML will be parsed as in the previous approach.
Last, but not least, you can use WebSockets for this purpose, or if both computers are in the same network, you can use sockets. Off course, a lot depends on the data source, whether you have access there, how modern is its technology and what does it allow you to do.

How do I manipulate data on a webpage from node.js

I'm kind of new to node.js and Javascript so forgive me if the question is poorly worded.
I'm using cheeriojs to scrape data from a site. I'm running the js file that scrapes the data in the command line and it outputs the data I want, but I want to be able to have access to this data on a webpage, where I can put it into a table. (or anything else I want for that matter). How exactly do I do this?
Thank you
Learn how to use express.
Serve the page to a browser, by reading the file and serving it with an express endpoint, or to curl or something. ie curl localhost:8080/your-endpoint
Using the browser manipulate the page inside of the browsers dev tools.
Figure out how to get the current DOM as an html string.
Figure out how to use the fetch API inside of a browser such as fetch(url, {method: 'POST'}).then(...)
After manipulating the DOM with the browsers dev tools.
POST the results back to express on the nodejs server.
Save the req.body or req.data or req.params to a file.
Side notes:
- figure out what a callback is, figure out how promises work
- do some research on express middleware
- look into setInterval, setTimeout
Related:
Node.js quick file server (static files over HTTP)
How do I get HTML corresponding to current DOM tree?
fetch(), how do you make a non-cached request?
What is Express.js?
https://github.com/Concatapult/node-catapult/blob/master/server/index.js
Writing files in Node.js
Other options, use a text editor, use sed, grep, awk, bash, or something else that doesn't require serving files to a browser?

Read json file in nested folders

I have director structure like this, only $(Root Directory)'s name is known at run time.
other folders and files are generated dynamically.(All files are .json)
my requirement is I need to count no of files and read content of all files using jquery and ajax.
I know if we have some static path like abc/xyz/somefile.json then we can read someFile.json but in my case I need to traverse nested folders.
help
You will not be able to do it without server-side support, either from the web server itself or another server-side process.
If directory listing is enabled on your web server, you can make an ajax request directly to the directory you want and scrape the returned document's content.
Another way would be to setup a web service which allows to query a directory's content. That service would be responsible for querying the file system and return the information to the client in a data-interchange format like JSON.

Reading files from hard drive with javascript

My application creates .xml files and stores them on the user's hard drive; there is a default folder that I set in the web.xml to store the files, let's say D:/temp. Now after I write the files I need to read them back with javascript, I am using a javascript library that has this function mxUtils.load('URL of the file') (this function returns the content of the file), the problem is that it is giving me an error Cross origin requests are only supported for HTTP, (I now it doesn't have anything to do with the function or the library) I think the problem is that you can't read local files because of some security issues. Anyone can advise me some solution? Thanks
You cant access local filesystem using javascript.
For accessing the file using javascript , you have to upload it to a server and access it using the files url.
As stated, you cannot access a file on the filesystem strictly through Javascript. You could, however, use the input file type to upload the file to your server and then read it:
<input type="file" name="myfileinput">
Then, you can access it via the $_FILES global in PHP - other languages also provide this functionality through other means. Please note, again, that there is absolutely no way to access a file that is on someone's filesystem with Javascript without their consent (i.e. using the file input type). That would be a huge security risk - imagine going to a page and having it wipe your whole D:/ drive.
It's best to expose your files via HTTP and use mxUtils.load("http://yoursite/static/yourfile.xml").
Search for static files on Apache HTTP Server HowTo. Setup Apache to serve your xml files, make sure that you can view xml file in browser and then use the same url in mxUtils.load call.

Read properties file located on server from Javascript

I have a properties file located in my server. Using Javascript or any other client side preferably JQuery, can I load it to fetch values? Is it possible?
The standard way to access server-side data from client-side Javascript is to pass it out through a controller on the server, preferably encoded in JSON, and use AJAX on the client side to request it from the server. If you don't need any security, you could possibly just expose the static, flat file from the server instead of making a full controller for it, and parse out the contents in Javascript.
There is no way to directly access server-side files since in general client-side code can't access the server's private file system.
If your properties file is accessible through a webserver (for eg: http://your-url/path/to/properties.xml) then you can simply host a script (http://your-url/path/to/script.html) and fetch the properties file using the AJAX and process it as a XML document (assuming the file is in XML format)
Refer to jQuery documentation on how to parse XML responses and use AJAX Object.
Read this API Doc: http://api.jquery.com/jQuery.ajax/ it has some examples to give you a head start in fetching documents using AJAX

Categories