AJAX Uncaught reference error

六眼飞鱼酱① 提交于 2021-02-10 20:18:24

问题


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.


回答1:


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.




回答2:


This line:

var request = new XMLHttprequest();

Should be:

var request = new XMLHttpRequest();
                       //^ Capital 'R'

Case in JavaScript, like most languages, matters




回答3:


    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




回答4:


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);
 }
}


来源:https://stackoverflow.com/questions/16293217/ajax-uncaught-reference-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!