arank
arank

Reputation: 221

XmlHttpRequest from an iframe to site running Django fails

I have an iframe from which I am trying to send a XMLHttpRequest. Here is the request code -

x=w.XMLHttpRequest?new XMLHttpRequest():(w.ActiveXObject?newActiveXObject('Microsoft.XMLHTTP'):0);
try{
if(!x)throw(0);
    x.open('GET','<url>',true);
    x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    x.onreadystatechange=function(){
    if(x.readyState==4){
        if(x.status==200){
            try{
                alert('hi');
            }
            catch(e){}
        }
        else {  }
    }
    };
    x.send('a=blah');   
}catch(e){}

The url it sends request to is implemented in Django. When I try to send this request on chorme, I get following error - "XMLHttpRequest cannot load iframe-url. Origin http://example.com is not allowed by Access-Control-Allow-Origin."

I found this middleware for django but that didn't work. I couldn't found a way to allow "Access-Control-Allow-Origin" in django documentation either. Any help appreciated.

Upvotes: 0

Views: 920

Answers (1)

arank
arank

Reputation: 221

This middleware do solves the problem - https://gist.github.com/426829 I added it in the wrong way.

Upvotes: 1

Related Questions