I know there are several questions how to save changes in an XML file using Javascript, and the obvious answer is: use a server side language such as PHP.
In my case, the XML file is in the same directory as my html file locally on my computer, not on any server.
In my index.html I access the alphabets.xml this way:
function getAlphabets() {
var xhttp;var result;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
result = findAlphabets(this);
console.log(result);
}
};
xhttp.open("GET", "alphabets.xml", true);
xhttp.send();
}
I want to change some data in the xml file, and save it back there. Is there any possible way to do so in plain javascript?
Or another way, not using a server side language?
Related
I am making a javascript library(hopefully I can complete it), and so I want to know the name of the sites that are using the script. I made an XML file <?xml version="1.0" encoding="UTF-8"?> <logs> <sites></sites> </logs>, with this code. I have the following code in the javascript library:
waste.runner = function(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "logs.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x;
x = xmlDoc.getElementsByTagName("logs")[0].getElementsByClassName('sites');
x.nodeValue += "site:"+window.location.href+";";
}
}
I looked through countless sites but none of them solve the problem, and I have reached this code. Is there any way I can change the data of an XML file using HTML and javascript?
Js can't do that.
The change happens within the browser, and it remains there.
You should make a request to the server and send the name of the website, and the server should save the input in some file or db.
I advice you to use the fetch API instead of XHR.
Your js will be:
fetch('example.php?website='+window.location.href)
inside fetch you will put the name of a file on your server.
Is it posible to request a JSON object from my browsers local storage with Ajax? it's just a simple object that i made with Js, and converted into JSON.
I then stored it to local browser storage, but i'm not sure that will work, considering that it might only work to request from a server.
I have seen simular questions about this, but i only see examples of jQuery, not pure JavaScript and AJAX.
<p id="demo"></p>
<script>
var info = {
name: "Josh",
age: 22,
born: "New York"
};
var jason = JSON.stringify(info);
localStorage.setItem("myJason", jason)
var http = new XMLHttpRequest();
http.open("GET", "file:///D:/HTML%20Files/Nettside%20med%20JSON%20og%20AJAX/nettside.html", true);
http.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
http.send();
If you are running the html file from the local disk then yes you can access it by navigating the file structure using "../" to go up a file, however if the HTML file has been loaded from a web server then the only way to access it is to use a file input and then read the file contents. The user must choose the file though.
Here is an article on reading binary data from a file that the user has selected.
https://www.html5rocks.com/en/tutorials/file/dndfiles/
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.open("GET", "https://zbapi.herokuapp.com/", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send();
This is my code. It is returning html text but i want it to load html output.
It is returning html text but i want it to load html output.
alert expects to be passed plain text.
You need to put the HTML source code somewhere which expects HTML source code.
For example, the innerHTML of an Element object.
The URL you are requesting, however, includes relative URLs and has its own JS. It doesn't make much sense to request it with JS and then try to render it. You would also certainly be better off just triggering navigation with location = "https://zbapi.herokuapp.com/".
You need to parsing HTML page to JSON with some parser on the server and send parsed data to the client in JSON format. For example, Himalaya (in Node.js).
Official repository of Himalaya
or use html2json (NPM Repository) to parse on client
So I know this is so lame to ask this question as I have spent a day searching on this subject without any success. As many others, I'm facing the crossdomain problem. The suggestion I see everywhere is to update the JSON file on a server or use localhost which I can't use because my assignment is not allowed to do so.
I am posting this question hoping there is some other solution for this.
I need to fetch data from a JSON file locally using pure JavasSript and Ajax which does not involve hosting on a server nor localhost (using absolute path is also a bad idea).
This is my code so far:
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', '/json/userinfo.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send(null);
}
(function() {
loadJSON(function(response){
var actual_JSON = JSON.parse(response);
console.log(actual_JSON);
})
})()
You won't be able to access local files using AJAX/XHR. Its not designed for this purpose.
What you can do is assign your json data into a variable in the json file,
var data = [{
}];
and then load your json file using script tag like below:
<script type="text/javascript" src="file_name.json"></script>
Now all the json data can be accessed using the data variable.
I'm working on an audio web application where you can play along with music and record yourself. I'm working with a recorder plugin and I'm trying to save what has been recorded in a folder on a server. I get a blob file from the plugin via this javascript code:
recorder.exportWAV(function(blob) {
var xhr = new XMLHttpRequest();
var url = '../../audio/recordings/test.wav';
xhr.open('POST',url,true);
xhr.onload = function(e) {
if (this.status == 200) {
console.log(this.responseText);
}
};
xhr.send(blob);
},'audio/wav');
I have never worked with this before so I'm not sure if my code is right. But I get no errors my file is just not saved. I have been searching the internet for this and what I have found is that a lot of people use a php file as url. Why? What php file are they using?
Thanks!