Skip to content Skip to sidebar Skip to footer

Xmlhttprequest.responsetext Doesnt Write The Value When Calling A Url

There may be a small error in my code. please advice me. I want to call a URL and display the value in div on pageload.I wrote this code from SO but the responseText doesnt write

Solution 1:

You cannot ajax a url from another domain unless it has implemented CORS

If you need to get data from somewhere which is not same origin you need to use JSONP

Also to debug, try calling the url from the locationbar to see if you receive valid data for your request

Solution 2:

req.onreadystatechange = function () { document.getElementById('divTxt').innerHTML = "My Status: " + req.responseText; }

you have to check, if the request was successful:

if (req.readyState === 4) { // what should be done with the result }

finally, it has to look like this:

req.onreadystatechange = function () { if (req.readyState === 4) { document.getElementById('divTxt').innerHTML = "My Status: " + req.responseText; } }

Post a Comment for "Xmlhttprequest.responsetext Doesnt Write The Value When Calling A Url"