This is my code
function load() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("change").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "https://www.example.com/xyz.txt", true);
xhttp.send();
}
The url for the xhttp.open is an example url to another domain with a txt file. What can I add to my code for this to work? Thanks.
The best way to do this is with CORS.
Related
What is wrong with this ajax function.
<script>
function loadDoc() {
var a=document.data.fitem.value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "master_find.jsp?val="+a) ;
xhttp.send();
}
</script>
I tried so many ways to supply a variable to the master_find.jsp script to retrieve the main item from a table., no success.
I've got some javascript code for replacing innerHTML of a element when clicked. Now i've got the code for that and it does work great! But one small issue is that, I want the webserver to remember the change when innerHTML of an element was replaced. So the webserver remembers the change and doesn't return to its default state. Whether with database or in Cookie, sessionStorage, localStorage.
document.getElementById("replace1").innerHTML = localStorage.getItem("replace1");
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
localStorage.setItem("replace1",this.responseText);
}
};
xhttp.open("GET", "https://natevanghacks.com/replacements/yoinkexecutor2.html", true);
xhttp.send();
}
Try something like this in AJAX:
localStorage.setItem("replace1",this.responseText);
And after body onload:
document.getElementById("replace1").innerHTML = localStorage.getItem("replace1");
Edit:
function loadSavedData(){
var data=localStorage.getItem("replace1");
document.getElementById("replace1").innerHTML = data?data:'No Data found';
}
function deleteData(){
localStorage.removeItem("replace1");
}
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
localStorage.setItem("replace1",this.responseText);
document.getElementById("replace1").innerHTML =this.responseText;
console.log(this.responseText);
}
};
xhttp.open("GET", "filename.html", true); //put your filename here...
xhttp.send();
}
<button onclick="loadDoc()">Send request and load Data
</button>
<button onclick="loadSavedData()">load Saved Data
</button>
<button onclick="deleteData()">Delete saved Data
</button>
<br>
<div id="replace1">
</div>
Note: You should run this file on http:// or https:// protocols only
Please tell me there is such a link. /add.php?id=2 How can I do it without reloading the page, but that the php file is executed.
use ajax
from:
https://www.w3schools.com/xml/ajax_intro.asp
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
}
You can do it with ajax.
http://api.jquery.com/jquery.ajax/
Javascript will execute that php file.
Ajax demo:
https://www.w3schools.com/xml/ajax_intro.asp
Don't overuse w3schools. I'm giving you this for demo purposes
I have this code that is changing the content of the DIV based on an external page:
external page
and
function loadexternalPage() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('main_frame').innerHTML =
xmlhttp.responseText;
}}
xmlhttp.open("GET", "externalpage.html", true);
xmlhttp.send();
}
How can I update this code in the way the function get a variable as an external page instead?
something like this??
Link to external page
function loadexternalPage(externalpage) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('main_frame').innerHTML = xmlhttp.responseText;
} }
xmlhttp.open("GET", externalpage, true);
xmlhttp.send();
}
or with method:
$(document).ready(function () {
loadxtPage("popup.html");
$("#main_frame").click(loadxtPage("externalpage.html"));
})
You can use the href attribute:
Pass the event here:
Link to external page
Use the events' attribute to get the URL:
function loadexternalPage(event) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('main_frame').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", event.target.href, true);
xmlhttp.send();
}
This is one possible solution. It really depends on where you need to specify the URL.
In order to conserve server resources I'm looking for a way to retrieve the content type of a given url using javascript. It should not download the complete content from the url only the headers. Is this possible with the restrictions javascript has.
Make an Ajax call with a head request.
var url = window.location.href;
var xhttp = new XMLHttpRequest();
xhttp.open('HEAD', url);
xhttp.onreadystatechange = function () {
if (this.readyState == this.DONE) {
console.log(this.status);
console.log(this.getResponseHeader("Content-Type"));
}
};
xhttp.send();
FYI if your server doesn't allow HEAD requests but does allow GET requests, you can do this by limiting the range of the GET request.
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
xmlhttp.setRequestHeader("Range", "bytes=0");
xmlhttp.onreadystatechange = function () {
if (this.readyState == this.DONE) {
console.log(this.getResponseHeader("Content-Type"));
}
};
xmlhttp.send();