ResponseString from XMLHttpRequest displays in alert but not in div



So I'm new and trying to display the output of a request as a div using the codes below:



<div id="myDiv"></div>
<button onclick="myFunction()">Try it</button> //button to test requeststring in alert box

<script>
function createCORSRequest(method, url){
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr){
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined"){
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
xhr.send();
return xhr;
}

var url = 'http://ift.tt/1D59bUX;
var xhr = createCORSRequest('GET', url);

document.getElementById("myDiv").innerHTML=xhr.responseText;

function myFunction() {
alert(document.getElementById(xhr.responseText);
}
</script>


The alert correctly display the responseText when a test button is clicked. However. I cannot display this text in my div using the document.getElementById("myDiv").innerHTML=xhr.responseText; line? Any solutions to do the latter?


No comments:

Post a Comment