I am new to using ajax. Now I have a project to create a localhost chatbox web page. And I want to save XML document using javascript. Here is my XML file.
<root>
<msg>Mesage 1</msg>
<msg>Message 2</msg>
</root>
Here is my javascript Code.
var xhttp = new XMLHttpRequest();
var doc;
xhttp.open("POST","det.xml");
xhttp.send(null);
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
doc = xhttp.responseXML;
var elem = doc.createElement("msg");
elem.textContent = "Hello XML";
doc.documentElement.appendChild(elem);
}
};
But after run this web XML document has no changed. Please tell me how to save xml using ajax. Thanks in advance. And sorry for my poor english.
Related
I have a simple code in a Javascript file that calls a PHP file using Ajax. At the moment it's a simple call and I don't need to pass any variables.
I'm using IIS and the folders structure is the following:
ROOT > sendMail.php
ROOT > JS > chat.js
The call starts from chat.js and it's the following:
function sendEmail(params){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("POST", "sendMail.php?q=" + params, true);
xmlhttp.send();
}
But I receive "404 Not Found". Someone can help me?
Thanks in advance!
I am finding a way to load multiple files at a time in my html document using vanilla AJAX ( I don't want any dependecy, the way I will get using jQuery ). Here is a piece of code I grabbed from W3schools' AJAX Documentation. :
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.querySelector("#tst").innerHTML = this.responseText;
}
}
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
I am pretty new to AJAX. So, I have a confusion. The above function works fine, but I have multiple files to load and I don't want to call this function multiple times, to load the files. Let me describe my desired output. I am imagining the syntax of the updated function will be :
loadDoc(["one.txt", "two.txt", "three.txt"])
So, we observe that the input will be an array. And now the ouput will follow this format :
content of "one.txt" + break + content of "two.txt" + break + content of "three.txt" and so on....
If the content of "one.txt" is "Hello World !", content of "two.txt" is "I love", and content of "three.txt" is "Javascript", the output will be :
Hello World !
I love
Javascript
So, can you please help me update my code ?
Pass the list of files to load to a PHP/ASP/whatever page which assembles the text for you.
function loadDoc(files) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.querySelector("#tst").innerHTML = this.responseText;
}
}
xhttp.open("GET", "ajax_info.php?files="+files, true);
xhttp.send();
}
The files parameter could be a comma delimited list, which the page uses to get the contents and assemble it into a single return string.
I have webpage on which i run a php file by javascript as given below
<script type="text/javascript">
var bhs = document.createElement('script');
var bhs_id = "yvw3lwc1tnvqfh4k4k4hisossew";
bhs.src = "//example.com/insert.php?site=" + bhs_id + "";
document.head.appendChild(bhs);
document.write("<span id='calc'></span>");
</script>
This JavaScript successfully insert data into insert.php file and then send to database. Besides i want to show one variable value generated in insert.php file in span id calc on a webpage where from above JavaScript is executed. How to do this? Thanks in advance.
It´s better use AJAX:
function loadDoc() {
var xhttp = new XMLHttpRequest();
var bhs_id = "yvw3lwc1tnvqfh4k4k4hisossew";
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert('OK!');
}
};
xhttp.open("GET", "//example.com/insert.php?site=" + bhs_id, true);
xhttp.send();
}
Reference:
http://www.w3schools.com/xml/ajax_intro.asp
i was trying to write a javascript program which read list of token from the configuration file and set the token. But javascript program failed to read my local configuration file.
javascript program below
require([
"splunkjs/mvc",
"splunkjs/mvc/simplexml/ready!"
],
function(mvc) {
var defaultTokenModel = splunkjs.mvc.Components.getInstance("default");
alert("start");
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.status == 200) {
var allText = xhttp.responseText;
alert('Text = ' + allText)
}
};
xhttp.open("GET", "rejected_panels.conf", true);
xhttp.send();
alert("end");
defaultTokenModel.set('home_panel_1', 'yes');
});
I am accessing javascript from splunk's simple xml as below
<form script="rejected_panels.js">
....
....
</form>
I got this error in chrome browser
rejected_panels.js:21 GET http://localhost:8000/en-GB/app/multi_tOP/rejected_panels.conf 404
Kindly Help me on this.
I am trying to read a XML file with Javascript , it is for a assignment in school so i cant use jQuery it has to be Javascript. Basicly i can read ONE value but not the other
This is my JS that is resposible for reading the XML value. It will read startLng but it gives me startLat is undefined. But if i check the XML file the startLat is not undefined. I cant see what the problem is here.
What i am trying to do is to get the LatitudeDegrees and LongitudeDegrees from the XML file. But it only gives me the LongitudeDegrees and says the LatitudeDegrees is undefined. What am i doing wrong here?
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
getLatLang(xmlhttp);
}
};
xmlhttp.open("GET", "G1.TCX", true);
xmlhttp.send();
}
function getLatLang(xml)
{
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("Trackpoint");
startLat = x[0].getElementsByTagName("Position")[0].getElementsByTagName("LatitudeDegrees")[0].childNodes[0].nodeValue;
startLng = x[0].getElementsByTagName("Position")[0].getElementsByTagName("LongitudeDegrees")[0].childNodes[0].nodeValue;
}
This is the XML file
<Trackpoint>
<Time>2008-10-28T15:58:22Z</Time>
<Position>
<LatitudeDegrees>59.4111992</LatitudeDegrees>
<LongitudeDegrees>13.5304104</LongitudeDegrees>
</Position>
<AltitudeMeters>85.6945801</AltitudeMeters>
<DistanceMeters>0.2149343</DistanceMeters>
<HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
<Value>116</Value>
</HeartRateBpm>
<SensorState>Absent</SensorState>
<Extensions>
<TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2" CadenceSensor="Footpod"/>
</Extensions>
</Trackpoint>
This has been solved.
The problem was i did not have a document load.