What difference between:
var xmlhttp = getXmlHttp()
and
var xmlhttp = new XMLHttpRequest()
?
If I correctly understand, each of this two cases create XRH object.
Please look at this function:
function getXMLHttp() {
var x = false;
try {
x = new XMLHttpRequest();
}
catch(e) {
try {
x = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(ex) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1) {
x = false;
}
}
}
return x;
}
getXMLHttp() is your custom function to simplify the creation an XHR object with the cross browser issues.
XMLHttpRequest is an object to used with the current modern browser. For the old browser like IE5 or IE6, you can use ActiveXObject("Microsoft.XMLHTTP");
However, the return object is the same for each browser.
If you open up Chrome Developer tools and try the following:
> getXmlHttp()
ReferenceError: getXmlHttp is not defined
This indicates that getXmlHttp is not a built in function.
Related
Script.js:
var request = new XMLHttprequest();
request.open('GET','data.txt',false);
if(request.status===200) {
console.log(request);
document.writeln(request.responseText);
}
This is my javascript file. I am getting this error:
Uncaught reference error:XMLHttprequest is not defined
Please help.
Sincere thanks.
I tried this it's simple mistake,
var request = new XMLHttpRequest();
dont use the simple letter for xmlHttpRequest. It should be a XMLHttpRequest. Also your simple r should be a capital R. it's work for me.Also try a different version of browser.
This line:
var request = new XMLHttprequest();
Should be:
var request = new XMLHttpRequest();
//^ Capital 'R'
Case in JavaScript, like most languages, matters
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");//this is for ie
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//this is for ie
} catch (E) {
try{
xmlhttp =new XMLHttpRequest();//for browsers other than ie
}
catch(e)
{
}
}
For browsers like ie XMLHttpRequest doesnt work
Try the following method to get your XML HTTP Request:
function GetXmlHttpObject()
{
try {
var xmlHttp = null;
if (window.XMLHttpRequest)
{
// If IE7, Mozilla, Safari, etc: Use native object
xmlHttp = new XMLHttpRequest()
}
else
{
if (window.ActiveXObject)
{
// ...otherwise, use the ActiveX control for IE5.x and IE6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
} catch(e)
{
alert(e.message);
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to return AJAX response Text?
How to return the response from an AJAX call from a function?
After whole day without solution to this. I read that this isn't possible or cant be done etc. I did go trough all of topics related, but cant find solution.
So i think my variable in code is out of scope and i cant return it, cant use it.
js file:
function getXMLHttp()
{
var xmlHttp
try
{
//Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
//Internet Explorer
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support AJAX!")
return false;
}
}
}
return xmlHttp;
}
function MakeRequest()
{
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4)
{
var myvar = new Array();
myvar=JSON.parse(xmlHttp.responseText);
return myvar;
}
}
xmlHttp.open("GET", "showimage.php", true);
xmlHttp.send();
}
So here above is ok, its returning value of php script when its finished.
And i want this myvar variable in html file. Tried like this:
window.onload = function() {
var myvar = MakeRequest();
alert (myvar);
}
It fails, returning undefined value.
Tried also making MakeRequest function as callback function, and then calling it from html like
MakeRequest(function(txt){
});
Then I got response, but can just alert it. I don't want to alert it. I need it to be stored in variable myvar because its needed later in code.
I have following code snippet:
self.xmlHttpReq = new XMLHttpRequest();
self.xmlHttpReq.onreadystatechange = function()
{
if(self.xmlHttpReq.readyState == 4 && self.xmlHttpReq.status == 200)
{
xmlDoc = self.xmlHttpReq.responseXML;
var xmlVar1 = xmlDoc.getElementsByTagName('var1')[0].childNodes[0].nodeValue;
var xmlVar2 = xmlDoc.getElementsByTagName('var2')[0].childNodes[0].nodeValue;
}
}
In IE the error code says:
object required, ajax request.js line num, char num
However, this same ajax request works fine in Firefox.
IE and Firefox have different object names for the XMLHttpRequest, you have to check your browser and declare the new object based on that.
Try something like this:
function getXHR() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("msxml2.XMLHTTP");
} catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xhr = false;
}
}
}
return xhr;
}
I got this from Jeremy Keith some time ago, it has never failed me.
Internet Explorer doesn't have the XMLHttpRequest object. Instead it uses an ActiveX object for the same functionality. So, you need to change this line:
self.xmlHttpReq = new XMLHttpRequest();
to:
if (window.ActiveXObject) {
try {
self.xmlHttpReq = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {
self.xmlHttpReq = new ActiveXObject('Msxml2.XMLHTTP'); // for really old versions of IE. You can leave the try/catch out if you don't care to support browsers from the '90s.
}
}
else
self.xmlHttpReq = new XMLHttpRequest();
I want to create xml file by DOM, that is what i wrote.
I run it in IE and it brings out a Permission denied error, and ActiveXObject is not defined in firefox in that line:
fso = new ActiveXObject("Scripting.FileSystemObject");
How can I fix that??
var xhttp;
try {
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (f) {
xhttp = null;
}
}
if (!xhttp && typeof XMLHttpRequest != "undefined") {
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", "nn.xml", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
newel = xmlDoc.createElement("student");
newtext = xmlDoc.createElement("stName");
newel.appendChild(newtext);
newtext2 = xmlDoc.createElement("examName");
newel.appendChild(newtext2);
newtext3 = xmlDoc.createElement("grade");
newel.appendChild(newtext3);
x = xmlDoc.documentElement;
x.appendChild(newel);
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("D:\\test\\test\\nn.xml", 2, true, -1); //2=Open a file for writing.
ts.Write(xmlDoc.xml);
ts.Close();
ActiveX is for Microsoft browsers only, you should NOT be using that if you want cross browser compatibility.
As for writing to a file, this is not allowed for a good reason. Consider how many times your hard-drive would have been erased while surfing random internet sites, if this was allowed.
We all know that XMLHttpRequest is supported by all major browsers.
Go google and if you find many different snippets to initiate it.
The one I use is:
function getNewHTTPObject()
{
var xmlhttp;
/** Special IE only code ... */
/*#cc_on
#if (#_jscript_version >= 5)
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
#else
xmlhttp = false;
#end #*/
/** Every other browser on the planet */
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
xmlhttp = false;
}
}
return xmlhttp;
}
Are there better ones ???
Really keen to embed the best way thanks.
By far the easiest method is to use a framework like jQuery which has all the cross-browser functionality you need.