IE8 AJAX GET setRequestHeaders does not work if parameters are not specified in the URL

I am trying to create an AJAX request in IE8.

var xhr = new ActiveXObject( 'Msxml2.XMLHTTP' );
xhr.open( 'GET', '/ajax/' );
// Required header for Django to detect AJAX request
xhr.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' );
xhr.onreadystatechange = function() {
   if ( this.readyState == 4 ) console.log(this.responseText); 
}
xhr.send( null );

This works great in Firefox, Chrome, Safari. However, in IE8, all of my AJAX test requests work EXCEPT for those where I execute GET without any query string parameters (like above). POSTs work without question, and GET requests only work when I include request strings in the URL, for example:

xhr.open( 'GET', '/ajax/?foo=bar' )

I am also 110% sure that my server-side code handles these requests correctly, so this completely crashes me.

Does anyone know what might cause this?

+3
source share
1 answer

, GET (a) . , MSIE8 , " ".

, GET , /freads/ . , , (, ?foo=bar.)

, jQuery HTTP- corrent, - :

var XMLHttpArray = [                        
    function() {return new XMLHttpRequest();},
    function() {return new ActiveXObject("Msxml2.XMLHTTP");},
    function() {return new ActiveXObject("Msxml2.XMLHTTP");},
    function() {return new ActiveXObject("Microsoft.XMLHTTP");}     
];

function XMLHTTPObject(){   
    var xmlhttp = false;
    for(var i=0; i<XMLHttpArray.length; i++){
            try{
                    xmlhttp = XMLHttpArray[i]();
            }catch(e){
                    continue;
            }
            break;
    }
    return xmlhttp;     
};

var http = XMLHTTPObject();
http.open(...);
0

Source: https://habr.com/ru/post/1741555/


All Articles