Ajax condition is not working in the jquery mobile phonegap - javascript

In Ajax the condition given to check the readystate and status is not working in the jquery mobile phonegap.I checked it by giving an alert message inside the condition. why? And I attached the code
function checkLogin()
{
// navigator.notification.alert("hiii");
var username;
var password;
var x=new XMLHttpRequest();
x.onreadystatechange=function()
{
// alert("....."+x.responseText);
if (x.readyState == 4 && x.status == 200)
{
// alert(x.readyState+" --- "+x.status);
alert("hai checkLogin");
var item=x.responseText.trim();
alert("Message :"+item);
if(item=="1")
{
window.location="home.html";
}
}
}
//alert("open");
x.open("POST", "http://localhost:8087/CarRental/com/selcar/getLogin.jsp?username="+document.getElementById('username').value+"&password="+document.getElementById('loginpaswrd').value, true);
x.send();

Related

Getting duplicate prints

I am facing some unidentified issue. please any one help me to sort out.
problem is when runonload() function runs, we will get print but I am getting duplicate prints (with same data). but this function executes one time (logs in this function write one time) but fnQrCode() & fnBuildBody() calling twice (logs in these function writes twice and getting prints twice). there is no loop for second time. Below is my code snippet.
var xmlcodebar="";
---
---
function runonload(){
code for print preparation
-------
-------
fnQrCode(dealref,sterlingQR, deliveryType); //this function for qr code generation
xmlcodebar = "data:image/png;base64,"+xmlcodebar;
footer11 = '<td width="100%"><img src='+xmlcodebar+' align="center" width="750" height="150"></td>';
footer12 = '</tr></table>';
fnBuildBody(strRows,strPrintData, Number(strPageLength),footer1,footer2, footer3, footer4, footer5, footer6, footer7, footer8,footer9, footer10, footer11, footer12); //this function for printing
-------
--------
}
function fnQrCode(dealref,sterlingQR,deliType){
var data =sterlingQR+";"+dealref+"$;"+deliType;
var strQueryString = "hdnOptPage=QRCODE&DATA="+data;
xmlHttp = GetXmlHttpObject();
if (xmlHttp == null) {
alert("Browser doesn't supports Http Request");
return;
}
var url = "<%=CONTROLSERV_TFROUT%>"+"?"+strQueryString;
xmlHttp.onreadystatechange = stateBarcode;
xmlHttp.open("POST",url,false);
xmlHttp.send();
}
function stateBarcode() {
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
if(xmlHttp.status == 404 || xmlHttp.status == 500){
alert("Problem occured at server. Please try again.");
window.event.returnValue=false;
return;
} else if (xmlHttp.status == 200) {
xmlcodebar = xmlHttp.responseText;
}
}
}
function GetXmlHttpObject() {
var objXMLHttp = null;
//check whether browser supports HttpRequest
if (window.XMLHttpRequest) {
objXMLHttp = new XMLHttpRequest();
} else {
if (window.ActiveXObject) {
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return objXMLHttp;
}

AJAX xmlhttp.open sumit data using POST method

I am trying to send a form data using POST method and xmlhttp.open
Please help find the error in my code
function submitChat() {
if (form1.msg.value == '' ) {
alert ('ALL FIELDS ARE MANDATORY');
return;
}
var xmlhttp = new XMLHttpRequest();
var msg = form1.msg.value;
var params = 'msg=helloooooooo';
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('POST', 'rtest.php?rid=14', true);
xmlhttp.send(params);
}
Everything is fine and it is working with GET method. but POST method is not working. Please help.

Run getElementById bound to success of AJAX call

I'm building a small quiz. The score is stored in a variable quizScore - each question on the quiz is output using AJAX. The single AJAX call grabs the ID of the button pressed (so, on question 2, the button ID is 2a which takes you to the next AJAX refresh of question2a.php)
On the last AJAX call, which calls up a file called questionendQuiz.php, I want to have the output of the variable quizScore. I need to bind this output to an event, so success of loading that through AJAX makes sense.
/* Output score */
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("quizWrapper").innerHTML=xmlhttp.responseText;
if (buttonID == "question_endQuiz") {
document.getElementById("scoreOut").innerHTML = quizScore;
}
}
So this should output quizScore into div id="scoreOut" when the successful AJAX content loads.
I've double checked the score is actually working by using console logs. It is building the score correctly (see here it outputs the score in the console http://francesca-designed.me/quiz/quiz.php
I can't work out what is wrong with the success if statement to cause it to not output the score?
AJAX request, I've omitted some things that aren't useful to this question like the correct answer scoring and the start of the quiz:
/* Quiz Score */
var quizScore = 0; /* Takes you to each next question, based on ButtonID */
function nextQuestion(buttonID) {
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("quizWrapper").innerHTML = xmlhttp.responseText;
}
}
var splitID = buttonID.split('_');
var questionID = splitID.pop();
xmlhttp.open("GET", "question" + questionID + ".php", true);
xmlhttp.send(); /* Output score */
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("quizWrapper").innerHTML = xmlhttp.responseText;
if (buttonID == "question_endQuiz") {
document.getElementById("scoreOut").innerHTML = quizScore;
}
}
}
The button that marks the end of the quiz is:
<button id="question_endQuiz" onClick="nextQuestion(this.id)">See your score</button>
The score recording part should go into onreadystatechange as well. It is the callback for the request, and gets executed after the response is received, rather than before as in the code you provided.
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("quizWrapper").innerHTML = xmlhttp.responseText;
if (buttonID == "question_endQuiz") {
document.getElementById("scoreOut").innerHTML = quizScore;
}
}
}

Ajax call not successful

I am making a javascript function call on onclick of any checkbox like this:
function getPGCountList(pageNo) {
var url = "someJsp.jsp?" + pageNo;
alert(1);
if (window.XMLHttpRequest) {
alert(2);
xmlhttp = new XMLHttpRequest();
} else {
alert(3);
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
alert(4);
xmlhttp.onreadystatechange = function () {
alert(5);
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(6);
document.getElementById("searchForPage").innerHTML = xmlhttp.responseText;
}
};
alert(7);
xmlhttp.open("GET", url, true);
alert(8);
xmlhttp.send();
}
The alert output I am getting is at my hosted site:
1-2-4-7-5-8-5-5-5
But in my local system it is:
1-2-4-7-5-8-5-5-5-6
I need to execute alert 6 also to change the content.
I am not sure where is the problem?
Your code looks fine to me. Check the path to someJsp.jsp
It's obviously not returning a normal response from the ajax call otherwise it would enter your if block and fire alert 6.
Just a thought too, but if you alert xmlhttp.readyState and xmlhttp.status maybe it'll help you find your problem. IF they are undefined, or refer to an object that is undefined, then your new XMLHttp requests failed. IF they give you results, you can see what the responses mean

cannot reach readystate=4

I go this AJAX code that worked fine, but when I moved it to a new file it stopped working.
I managed to find out that the readystate isn't 4 and status isn't 200.
The code was given to my in class by the teacher. It worked find until I made new file.
<script type = "text/javascript">
var request = false;
if (window.XMLHttpRequest)
request = new XMLHttpRequest();
else if (window.AciveXObject)
request = new ActiveXObject("Microsoft.XMLHTTP");
function login(PhpFile,divId,frm)
{
if (request)
{
var obj = document.getElementById(divId);
request.open("POST",PhpFile);
//setting the header
request.setRequestHeader('Content-Type','application/x-www-form- urlencoded');
request.onreadystatechange = function()
{
if (request.readyState == 4 &&
request.status == 200){
obj.innerHTML =request.responseText;
}
else
{
alert("here");
}
}
request.send("log="+frm.log.value+"&pwd="+frm.pwd.value);
}
}
</script>
if you alert(request.responseText) in that else case, that may give you more information
'application/x-www-form- urlencoded'
should be
'application/x-www-form-urlencoded'

Categories

Resources