0
<input type="text" onkeyup="checkPin();" id="pin"/>

hi all i am new in django and i am trying to access database by views def pincheck(): and i am trying this by javascript but ther is some error is occurred.

function checkPin(){
    var pin_code=document.getElementById("pin").value;
    if(pin_code.length == 6){        
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("innerHTML").innerHTML=xmlhttp.responseText;
            }
            else if (request.status === 404) {  
                alert("Oh no, it does not exist!");
            }  
            else if (request.status === 403) {  
                alert("Oh no, it does not exist!");
            }  
        }
      var data = "{% csrf_token %}";
      xmlhttp.setRequestHeader('X-CSRF-Token', data);
      xmlhttp.open("POST", "../../sellerprofile/ajaxcall/");
      xmlhttp.send();
    }
}

this is my javascript please correct me if wrong. the error is Uncaught InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.

4
  • 3
    Use jquery.. it is simple , clean.. easy.. Commented May 9, 2016 at 14:40
  • i could not use jquery.. Commented May 9, 2016 at 14:45
  • 1
    i have to use vanilla js Commented May 9, 2016 at 14:45
  • though the title says django, this doesn't seem to have anything at all to do with django. and as @ZohaibIjaz use jquery. Commented May 9, 2016 at 14:50

1 Answer 1

2

The problem with your code is you are setting the header without actually opening the connection that's why it is giving InvalidStateError. The right way to do is first open the connection then set the header. Below is your modified code.

function checkPin(){
var pin_code=document.getElementById("pin").value;
if(pin_code.length == 6){        
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else{// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            document.getElementById("innerHTML").innerHTML=xmlhttp.responseText;
        }
        else if (request.status === 404) {  
            alert("Oh no, it does not exist!");
        }  
        else if (request.status === 403) {  
            alert("Oh no, it does not exist!");
        }  
    }
  var data = "{% csrf_token %}";
  xmlhttp.open("POST", "../../sellerprofile/ajaxcall/");
  xmlhttp.setRequestHeader('X-CSRF-Token', data);
  xmlhttp.send();
}

}

I hope it will work ;)

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much for giving me this information... now its working fine... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.