Reputation: 33
Just trying to get a simple piece of javascript to work that outputs the contents of a file in the same directory as an alert.
<form id="form" method="post" onsubmit="newsubmission()">
<input type="submit" value="Submit" />
<script>
function newsubmission() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { alert(xhttp.responseText); }
xhttp.open("POST", "test.txt", true);
xhttp.send();
}
</script>
</form>
When I click the Submit button a message is displayed with no text. I've been working on this all day and tried every online tutorial without success. Finally out of ideas. Can someone help?
Upvotes: 0
Views: 481
Reputation: 98
it's contain some errors and i have fixed it now try this
<form id="form" method="post">
<input type="submit" value="Submit" />
<script>
document.getElementById('form').onsubmit = function(e){
e.preventDefault();
var xmlhttp;
//for IE
if(window.ActiveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}else{
// for modern browsers
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("POST", "text.txt", true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
alert(xmlhttp.responseText);
}
}
xmlhttp.send();
}
</script>
</form>
Upvotes: 0
Reputation: 244
Better and easy to use a jQuery ajax
$.ajax({
url:"your url",
success:function(data){
//do your stuff with response data here
}
error:function(){
//handle your error
}
});
Upvotes: 1
Reputation: 98
Cause with your ajax syntax. Try this
function newsubmission(e) {
e.preventDefault();
var xhttp = new XMLHttpRequest();
//for IE
if(window.AciveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}else{
// for modern browsers
xmlhttp = new XMLHttpRequset();
}
xhttp.open("POST", "test.txt", true);
xhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
alert(xmlhttp.responseText);
}
}
xhttp.send();
}
Upvotes: 0