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);
}
}
Related
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.
I have below code that handling the HTTP request. But I am getting
Error: InvalidStateError:DOM Exception 11
error.
if (window.XMLHttpRequest) {
req_settings = new XMLHttpRequest();
req_settings.onreadystatechange = processChange;
req_settings.open("GET", url, true);
req_settings.send();
} else if (window.ActiveXObject) {
req_settings = new ActiveXObject("Microsoft.XMLHTTP");
if (req_settings) {
req_settings.onreadystatechange = processChange;
req_settings.open("GET", url, true);
req_settings.send();
}
}
req_settings.onreadystatechange = processChange;
req_settings.send();
Please help.
You're calling send() twice, which is invalid. Your code should be
if (window.XMLHttpRequest) {
var req_settings = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req_settings = new ActiveXObject("Microsoft.XMLHTTP");
} else
throw "environment does not support ajax";
req_settings.onreadystatechange = processChange;
req_settings.open("GET", url, true);
req_settings.send();
The code makes no sense, you would be calling
req_settings.onreadystatechange = processChange;
req_settings.send();
twice, Does it inside the if and outside of it! Remove the ones inside. Also it should be using the native object. Use a library!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript ajax request without framework
How can I make the JQuery Ajax call below without using JQuery or any other library but a by only using JavaScript built-in functions?
var input = '{
"age":100,
"name":"foo",
"messages":["msg 1","msg 2","msg 3"],
"favoriteColor" : "blue",
"petName" : "Godzilla",
"IQ" : "QuiteLow"
}';
var endpointAddress = "http://your.server.com/app/service.svc";
var url = endpointAddress + "/FindPerson";
$.ajax({
type: 'POST',
url: url,
contentType: 'application/json',
data: input,
success: function(result) {
alert(JSON.stringify(result));
}
});
jQuery does a good job normalizing all the little quirks and nuonces between browsers for ajax calls.
I'd suggest finding a stand-alone ajax library that can do the same thing but without all the extra overhead jQuery brings with it. Here are a few:
Reqwest
Fermata
Ajax (inspired by jquery/zepto)
Micro Ajax
Try this Example
First You have to create object of window.XMLHttpRequest or ActiveXObject (for IE)
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
Then You can send the request
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
At last You can Get the responce
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
The code below does everything that your jQuery version does:
POST request with JSON as postdata
Sets the JSON Content-type header
Alerts the stringified response
Code:
var httpRequest;
function makeRequest(url, input) {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function(){
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
alert(JSON.stringify(httpRequest.responseText));
}
}
};
httpRequest.open('POST', url);
httpRequest.setRequestHeader('Content-Type', 'application/json');
httpRequest.send(input);
}
var input = '{
"age":100,
"name":"foo",
"messages":["msg 1","msg 2","msg 3"],
"favoriteColor" : "blue",
"petName" : "Godzilla",
"IQ" : "QuiteLow"
}';
var endpointAddress = "http://your.server.com/app/service.svc";
var url = endpointAddress + "/FindPerson";
makeRequest(url, input);
Taken partly from MDN.
Take a look at Google's first answer over here.
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();
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.