I'm working on a website in which I need to make a GET request to a webservice and log the response. I'm trying the below code in jsp.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
var a = this.responseText;
alert(a);
console.log(a);
}
...
xhttp.open("GET", "https://example.com/test.ashx", true);
xhttp.send();
I am able to see the response in Firefox networks tab. But alert/console log doesn't work. What am I doing wrong here?
try this
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == XMLHttpRequest.DONE) {
var a = this.responseText;
alert(a);
console.log(a);
}
}
xhttp.open("GET", "https://example.com/test.ashx", true);
xhttp.send();
Related
I have been trying to read a JSON file from google cloud storage from my HTML
but he didn't work.
This is my json context:{'first_name': 'Anat'}
How can I get the context file?
This is my part of my html context:
<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.first_name;
}
};
xmlhttp.open("GET", "https://storage.googleapis.com/{backetname}/{filename}.json", true);
xmlhttp.send();
</script>
I've been struggling for hours with following code without success. In my html I have several inputs (type=text, type=date and selects), and a button calling a js function: onclick=SendNewData().
The JS function is something like the following:
function SendNewData() {
var MyData1=document.getElementById("id1").value;
var MyData2=document.getElementById("id2").value;
var MyData3=document.getElementById("id3").value;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open('POST', 'Handler.php', true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status==200) {
document.getElementById("FormNuevaCom").innerHTML = xmlhttp.responseText;
}
}
var data = new FormData;
data.append('DATA1', MyData1);
data.append('DATA2', MyData2);
data.append('DATA3', MyData3);
xhr.send(data);
}
and the Handler.php is something like the following:
if(isset($_POST['DATA1'])) {
$MyVar=$_POST['DATA1'];
echo "Hi there! ".$MyVar." received...";
}
I canĀ“t get any response. Anyone can spot the problem?
function ajax() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
alert(this.responseText);
}
};
}
xhttp.open("POST", "abcdef.xyz/abc/logincheck.php?mail=abc#gmail.com&password=abc", true);
xhttp.send();
<p id="demo">
The content of the body element is displayed in your browser.
</p>
<button onclick="ajax()">
Click on me!
</button>
I have a code as seen above and the ajax operations do result in failure. What is wrong with my code? The text in p never changes.The php file starts like this:
<?php
$mail = $_POST["mail"];
$password = $_POST["password"];
xhttp.open() and xhttp.send() need to be inside the ajax() function.
function ajax() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
alert(this.responseText);
}
};
xhttp.open("POST", "abcdef.xyz/abc/logincheck.php?mail=abc#gmail.com&password=abc", true);
xhttp.send();
}
due to xhttp.open and send are define out side of function block.
xhttp.open("POST", "abcdef.xyz/abc/logincheck.php?mail=abc#gmail.com&password=abc", true);
xhttp.send();
I have written a code in script which is given below.
function myFunction(a) {
var k=a;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("ques").innerHTML = this.responseText;
}
};
xhttp.open("GET", "retrieveQuestion.php?question=" +a, true);
xhttp.send();
var q="<td><input type='text' name='answer' id='ans' placeholder='submit your answer here'/></td>" ;
document.getElementById("t2").innerHTML=q;
var answ = document.getElementById("ans").value;
var z="<td align='center'><input type='button' value='submit' onclick='myfunc1(k,answ)'/></td>";
document.getElementById("t3").innerHTML=z;
}
function myfunc1(q1,a1)
{
xhttp.open("GET", "checkanswer.php?question=" +q1 + "&a1=" +a1, true);
xhttp.send();
}
Now when i click on submit button it did not redirected to checkanswer.php
can anyone help me in this problem.
Using xhttp.open is sending a background request to checkanswer.php, rather than actually redirecting to it. You'd need to use something like window.location instead.
I'm trying to populate an HMTL page with XML data. This code used to work but I must have changed something because now nothing happens on the button clicks. Could it potentially be a problem with the server? And in that case what changed? The line where things appear to stop working is getElementsByTagName, all alerts before that line still work.
Here's the scripts:
<script type="text/javascript">
function loadXMLDocTraditional() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
//alert("Xhttp.readyState = " + xhttp.readyState);
var xmlDoc = xhttp.responseXML;
//There is something wrong with this line
var item = xmlDoc.getElementsByTagName("trad")[0].childNodes[0];
document.getElementById("data").innerHTML = item.nodeValue;
}
};
xhttp.open("GET", "data.xml", true);
xhttp.send();
}
function loadXMLDocSimple() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
//alert("Xhttp.status = " + xhttp.status);
if (xhttp.readyState == 4 && xhttp.status == 200) {
var xmlDoc = xhttp.responseXML; //Using a variable doesn't work bc/ of the getElements method?
var item = xmlDoc.getElementsByTagName("simp")[0].childNodes[0];
document.getElementById("data").innerHTML = item.nodeValue;
}
};
xhttp.open("GET", "data.xml", true);
xhttp.send();
}
</script>
And here's the XML:
<dictionary>
<word>
<title>Ubiquitous</title>
<trad>This is the traditional defintion ubiquitous</trad>
<simp>This is the simplified defintion hopefully ubiquitous</simp>
</word>
<word>
<title>Lithe</title>
<trad>This is the traditional defintion of lithe</trad>
<simp>This is the simplified defintion of lithe hopefully</simp>
</word>