<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.
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 ;)
Related
in a webpage i would like to collect a response from another web server at a given URL address.
let's say someone else has a server at http://mysite/123 that responds with a simple string. (without headers and stuff).
what is the most SIMPLE way to get javascript on my webpage to collect a url's raw response in preferably a byte array variable? though i would except an answer that saves in string to get me going. this is an exact copy paste from my html document and its not working for me.
thanks!
<script>
var txt = "";
txt=httpGet("https://www.google.com");
alert(txt.length.toString());
function httpGet(theUrl) {
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) {
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false);
xmlhttp.send();
}
</script>
So I'd have to say your best bet would be to look into making an HTTP (or XHR) request from javascript.
check: Return HTML content as a string, given URL. Javascript Function
function httpGet(theUrl)
{
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)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false );
xmlhttp.send();
}
I done a ajax call to local http server but I got error in xmlhttp.open("GET", "http://localhost//push", true); incorrect function in IE 11, Below is my full code:
var xmlhttp;
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) {
....some code.....
}}
}
xmlhttp.open("GET", "http://localhost//push", true);
xmlhttp.send();
}
IE uses new XMLHttpRequest();
Finally i got the answer very silly issue, In chrome URL can be "http://localhost//push" but in IE it should have backslash instead of forward slash "http:\\localhost\\push" or else it will show incorrect function
I keep running the following code but keep getting the "Bad connection" alert pop up, meaning I didn't get a successful response code.
Does anyone see anything wrong with the following script?
function process(){
URL = document.getElementById("userInput").value;
var xmlhttp;
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("Response").innerHTML=xmlhttp.responseText;
alert("Successful connection!");
}
else {
alert("Bad connection!");
}
}
xmlhttp.open("GET", URL, true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
}
The readyState passes through a number of other states (opened, sent, headers received, loading) before reaching state 4 (done). Each time the event fires, you'll get the Bad Connection alert until it is successful.
You probably want something more like:
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
document.getElementById("Response").innerHTML=xmlhttp.responseText;
alert("Successful connection!");
} else {
alert("HTTP error: " + xmlhttp.status);
}
}
How and where do I add the message "Server not available" if I have a code that calls a php file from a remote server but the php file is unavailable? Here's my code:
<html>
<head>
<script language="Javascript">
function View(){
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("datatable").innerHTML=xmlhttp.responseText;
}
xmlhttp.open("POST", "http://someremoteserver/somephpfile.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send();
}
</script>
</head>
<body onload="View();" >
<div id="datatable" align="center"></div>
</body>
</html>
You could detect the different HTTP Status codes and do something based on the response. Or you could just implement a generic handler if the status isn't 200:
if (xmlhttp.readyState==4) {
if (xmlhttp.status==200) {
document.getElementById("datatable").innerHTML=xmlhttp.responseText;
} else if (xmlhttp.status == 503) { //specific error code
alert('Sorry, this server is unavailable');
} else { //generic error handler
alert('There was an error with your request');
}
}
I am trying to check if the given url is working or not using ajax. So i modified the Ajax a little bit.
function isServerAlive()
{
var xmlhttp;
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)
{
document.getElementById("myDiv").innerHTML=xmlhttp.status;
}
}
xmlhttp.open("GET","wrongurl",true);
xmlhttp.send();
}
All i want to do is it to display the xmlhttp.status as 404 or 503 when the url is wrong. But it is not printing. Any suggestions?
You have status code available in .status and the text in .statusText
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState == 4)
console.log("Code: " + xmlhttp.status + "Status text: " + xmlhttp.statusText);
}
a respone object is passed to the xmlhttp events, so use this instead:
xmlhttp.onreadystatechange=function(r) {
if (r.readyState == 4) {
document.getElementById("myDiv").innerHTML = r.status;
}
}
I don't think you'd be able to use XHR to check if url is working, as the url has to be from the same origin as the original page. First check if your solution works correctly when accessing a local url.