I would like to use data within a JSON file which I get by using the XMLHttpRequest. I already checked that I recieve the file.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
var obj = xhttp.open("GET", "../data/data.json", true);
xhttp.send();
var obj1 = JSON.parse(obj);
a0 = obj1.a0;
This is my JSON file.
{"a0":2, "a1": -2.356, "a2": 4.712}
I can't find the mistake I am doing here. Can you help?
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
var obj=this.responseText;
var obj1 = JSON.parse(obj);
a0 = obj1.a0;
}
};
xhttp.open("GET", "../data/data.json", true);
xhttp.send();
You need to get the response text inside the xhttp response.
onreadystatechange is a callback. That means that it's called asynchonously when the request ended. SO a part of your code is misplaced. Here the correction:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
var obj1 = JSON.parse(this.responseText);
var a0 = obj1.a0;
}
};
xhttp.open("GET", "../data/data.json", true);
xhttp.send();
Related
I'm trying to call this JS function inside a php page (footer.php), but it's not working. I've tried multiple options, like placing the script inside a php echo, but that didn't work either. I'm stuck here. Any suggestions to call it properly?
<?php
/**
* Header for our theme
*/
electro_get_footer();
?>
<div class="kiyoh">
<span id="cijfer" class="review-cijfer"></span>
<span class="review-cijfer-after"> / 10</span>
<a href="https://kiyoh.nl/lumenlab">
<span id="beoordelingen" class="review-beoordelingen"></span>
<span class="review-beoordelingen-after"> beoordelingen</span>
</a>
</div>
<script type="text/javascript">
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET",
"https://www.kiyoh.nl/xml/recent_company_reviews.xml? connectorcode=xws9TE3TQSX7t7Hrj9xFAbYwhraBaenGFHYWt9jKyx4CRV5vFW&company
_id=16907&reviewcount=all&showextraquestions=1", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById("cijfer").innerHTML =
xmlDoc.getElementsByTagName("total_score")
[0].childNodes[0].nodeValue;
document.getElementById("beoordelingen").innerHTML =
xmlDoc.getElementsByTagName("total_reviews")
[0].childNodes[0].nodeValue;
}
</script>
I have change the url in the xhttp.open() function and it work.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
} else {
console.log("readyState: " + this.readyState);
console.log("Status: " + this.status);
}
};
xhttp.open("GET", "https://www.kiyoh.nl/xml/recent_company_reviews.xml?connectorcode=xws9TE3TQSX7t7Hrj9xFAbYwhraBaenGFHYWt9jKyx4CRV5vFW&company_id=16907&reviewcount=all&showextraquestions=1", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById("cijfer").innerHTML = xmlDoc.getElementsByTagName("total_score") [0].childNodes[0].nodeValue;
document.getElementById("beoordelingen").innerHTML = xmlDoc.getElementsByTagName("total_reviews") [0].childNodes[0].nodeValue;
}
i have a php page on my server that controls my mysql database. there is also another page that displays everything. i downloaded that page into my pc and modified the code for it to be still able to connect to that php file on my server that controls the database.
when the html page is on the server it works fine. but, when i downloaded it it does not work anymore. here is some of the code:
btw, all functions are written in javascript.
function queryNumberOfQuestions() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var numberOfQuestions = parseInt(this.responseText);
populateSequence(questionOrder, numberOfQuestions);
shuffle(questionOrder);
}
};
xhttp.open("GET", "http://wael-alghamdi.com/getQuestionCount.php", true);
xhttp.send();
}
function queryNextQuestion(questionNumber) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
theQuestion = this.responseText.split(',');
updateGame();
}
};
var url = "http://wael-alghamdi.com/getQuestion.php?row=" + questionNumber.toString();
xhttp.open("GET", url, true);
xhttp.send();
}
function queryOtherAnswers(questionNumber) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
otherAnswers = this.responseText.split(',');
updateGame();
}
};
var url = "http://wael-alghamdi.com/getOtherAnswers.php?row=" + questionNumber.toString();
xhttp.open("GET", url, true);
xhttp.send();
}
what i changed was those three lines:
xhttp.open("GET", "http://wael-alghamdi.com/getQuestionCount.php", true);
var url = "http://wael-alghamdi.com/getQuestion.php?row=" + questionNumber.toString();
var url = "http://wael-alghamdi.com/getOtherAnswers.php?row=" + questionNumber.toString();
so my question is how can i make it work even if the php is on a different machine?
What is the correct way to call this api? The url of the api is: https://www.bitstamp.net/api/ticker/ ?
I want to read the "high" value...
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.high;
}
};
xmlhttp.open("GET", "https://www.bitstamp.net/api/ticker/", true);
xmlhttp.send();
</script>
What am i missing? Thx!!
Adding a timestamp to the URL made it work for me:
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.high;
}
};
xmlhttp.open("GET", "https://www.bitstamp.net/api/ticker/?u" + new Date().getTime(), true);
xmlhttp.send();
</script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.high;
}
};
xmlhttp.open("GET", "https://www.bitstamp.net/api/ticker/?u" + + new Date().getTime(), true);
xmlhttp.send();
<p id="demo"></p>
I have a parsed xml-file that shows twice the same element. Now I want a button that hides one of them with an onclick-statement. Does anyone know how to do this?
<!DOCTYPE html>
<html>
<body>
<p id="dasa"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "customers.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("syl");
document.getElementById("dasa").innerHTML =
x[0].getAttribute('category') + "<br>";
document.getElementById("dasa").innerHTML +=
x[0].getAttribute('category');
}
function remove() {
x[0].removeAttribute('category');
}
</script>
<button onclick="remove()">remove</button>
</body>
</html>
x is undefined in your remove function.
function remove() {
x[0].removeAttribute('category');
}
You want something like this:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "customers.xml", true);
xhttp.send();
var xmlDoc;
var x;
function myFunction(xml) {
xmlDoc = xml.responseXML;
x = xmlDoc.getElementsByTagName("syl");
document.getElementById("dasa").innerHTML =
x[0].getAttribute('category') + "<br>";
document.getElementById("dasa").innerHTML +=
x[0].getAttribute('category');
}
function remove() {
x[0].removeAttribute('category');
}
This will make x into a global var set by myfunction.
Iam new to Phonegap, Iam not able to get the Response while Parsing the XML Url.what i have tried is,
function initLoginPage() {
var xmlhttp = new XMLHttpRequest();
var urlString = "url/app/api.php?fn=employees";
xmlhttp.onreadystatechange = processData;
xmlhttp.open("GET", urlString, true);
xmlhttp.send();
}
function processData() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("result").innerHTML=xmlhttp.responseText;
} else{document.getElementById().innerHTML = "<b>Please Wait..</b>";}
}
I have called initLoginPage function when device is Ready.I have to show the Response in listView. iam using JQuery Mobile.The Response of Url is
<?xml version="1.0" encoding="UTF-8" ?>
<details>
<responsecode>200</responsecode>
<Employee>
<name>name</name>
<Fathername>fathename</Fathername>
<Address>address</Address>
<Number>12342456</Number>
<Enumber>4324421341234</Enumber>
<OfficeMail>test#gmail.com</OfficeMail>
<PersonalMail>test#gmail.com</PersonalMail>
<EmployeeID>1</EmployeeID>
<DOB>21-06-1991</DOB>
<DOJ>05-03-2013</DOJ>
<PanNumber>123456</PanNumber>
<image>./images/1380372683.png</image>
</Employee>
</details>
Yo have do mistake
var urlString = url+"/app/api.php?fn=employees";
The variable xmlhttp is only defined in the scope of the initLoginPage function, so it isn't defined in processData.
There are two ways to solve your Problem. You use this instead of xmlhttp in processData:
function initLoginPage() {
var xmlhttp = new XMLHttpRequest();
var urlString = "url/app/api.php?fn=employees";
xmlhttp.onreadystatechange = processData;
xmlhttp.open("GET", urlString, true);
xmlhttp.send();
}
function processData() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("result").innerHTML = this.responseText;
}
else {
document.getElementById().innerHTML = "<b>Please Wait..</b>";}
}
}
Or pass processData as an anonymous function directly to xmlhttp.onreadystatechange:
function initLoginPage() {
var xmlhttp = new XMLHttpRequest();
var urlString = "url/app/api.php?fn=employees";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("result").innerHTML = xmlhttp.responseText;
}
else {
document.getElementById().innerHTML = "<b>Please Wait..</b>";}
}
}
xmlhttp.open("GET", urlString, true);
xmlhttp.send();
}