How to call same ajax until success response is not empty - javascript

Am trying to get data from ajax if success data is empty need to call same ajax until ajax success data is returned.
Can some one help me to call ajax ever 2 sec until ajax returns data.
this.submitPax = function() {
function getCityName() {
var country = 1;
//var ajaxUrl = "<?php echo WEB_URL_ADMIN; ?>checkdriverstatus";
tjq.ajax({
url: baseUrlIs + "checkdriverstatus",
dataType: 'json',
type: 'post',
data: { "countryID": country },
success: function(response, textStatus, request) {
//alert(response);
if (response != '') {
tjq("div#divLoadingSeat").removeClass('show');
} else {
alert('Waiting for driver to accept job');
//setTimeout(function(){getCityName();}, 1000);
tjq("div#divLoadingSeat").addClass('show');
return false;
}
},
});
}
}

Related

AJAX POST Request execute only first call on safari

$('#course_id').change(function() {
var courseid = $(this).val();
getuniqueid(courseid);
//alert(courseid);
});
function getuniqueid(courseid){
if (courseid == '0')
{
alert("Please select the Course.");
document.getElementById("course_id").value = "<?php echo $id ?>";
document.getElementById("course_id").focus();
return false;
}
$.ajax({
type: "POST",
url: 'createuniqueid.php',
data: {
courseid:courseid
},
datatype:'json',
cache: false,
success: function( data ) {
//alert(data);
$('#custom').val(data);
console.log("success");
}
});
}
function coupon_submit(elm){
var sender_name = document.getElementById("sender_name").value;
var sender_email_id = document.getElementById("sender_email_id").value;
var receiver_name = document.getElementById("receiver_name").value;
var receiver_email_id= document.getElementById("receiver_email_id").value;
var courseid = document.getElementById("course_id").value;
var uniqueid = document.getElementById("custom").value;
$.ajax({
type: "POST",
url: "datastring.php",
data: { sender_name: sender_name,
sender_email_id: sender_email_id,
receiver_name: receiver_name,
receiver_email_id:receiver_email_id,
courseid:courseid,
uniqueid:uniqueid
},
cache: false,
success: function( data ) {
alert(data);
console.log("success....");
},
error: function(jqXHR, textStatus, errorThrown,data) {
console.log("error. textStatus: %s errorThrown: %s", textStatus, errorThrown);
}
});
}
It works fine on chrome, IE, Firefox but not on Safari. On Safari it executes only first AJAX call.
I need to call first getuniqueid and then coupon_submit. But for debugging process I placed coupon_submit function's code in getuniqueid function, the code of datastring.php get executed. Means it executes first AJAX call only.
Appriciate any help in advance!!

Ajax. On Ajax success call same ajax function one more time

This is what i tried.
tjq.ajax({
type: 'POST',
url: '<?php echo base_url();?>getCmsHotel?t=<?php echo $traceId;?>',
dataType: 'JSON',
encoding:"UTF-8",
contentType: "application/json",
traditional: true,
async: true,
error: function (request, error) {
searchApiCount++;
hotelssearchObj.reloadFunctions(searchApiCount);
return false;
},
success: function (data) {
//alert(data.status);
if(data.status == 'FAILURE'){
//searchresults = data;
searchApiCount++;
hotelssearchObj.reloadFunctions(searchApiCount);
return false;
}else if(data.status == 'SUCCESS'){
var recalajx = '2';
if(recalajx =='2' && recalajx!=3){
recalajx ='3';
tjq.ajax(this);
}
alert(recalajx);
tjq('.searchresultsDiv').remove();
hotelsresults = data;
//hotelssearchObj.hotelsResults(data);
gblStartCount = 1;
gblHotelData = tjq.extend(true, {}, data);
gblHotelDisplayData = tjq.extend(true, {}, data);
hotelssearchObj.hotelsResults(gblHotelDisplayData);
searchApiCount++;
hotelssearchObj.reloadFunctions(searchApiCount);
tjq("div#divLoading").removeClass('show');
}
}
});
This code calling multiple times. Am trying to call tjq.ajax(this); only once after 1st ajax SUCCESS.
when tried to alert getting 3 but still axaj calling for multi times.
How to stop this can some help!
One solution is to put the Ajax call in a function, and check how many times it has been called with a counter. If the counter is less than 2, call the function again.
here's an example:
ajaxCall();
function ajaxCall(counter = 0) {
$.ajax({
type: 'POST',
success: function() {
counter++
if (counter < 2) {
ajaxCall(counter);
}
}
});
}

how to stop ajax request if another request exist

i'm trying to make infinite scrolling so when scrolling i make an ajax request to the server to get data but when scrolling a multiple ajax request is made and return the same data so how can i cancel ajax request before sending if there one already exist i tried like this
data: ({
beforeSend: function (xhr) {
if (activeAjaxConnections != 1) {
xhr.abort();
}
activeAjaxConnections++;
//Show Loader....
$("#Ajax-Load-Image").css('visibility', 'visible');
},
all my code
var lock_load = '1';
var activeAjaxConnections = 1;
var PageNumber = 2;
$(window).scroll(function () {
if ((Math.ceil($(window).scrollTop() - $(window).height()) * -1) <= getHeight() + 550) {
if (lock_load === '1') {
var xhr = $.ajax({
type: "POST",
async: true,
dataType: "json",
url: ajaxurl,
data: ({
beforeSend: function (xhr) {
if (activeAjaxConnections != 1) {
xhr.abort();
}
activeAjaxConnections++;
//Show Loader....
$("#Ajax-Load-Image").css('visibility', 'visible');
},
type: "POST",
action: 'Ajax_Get_SpacesAndSponsors',
Page: PageNumber
}),
success: function (response) {
PageNumber++;
var Message = response.spaces.Message;
console.log(response);
console.log(Message);
Draw_SpacesAndSponsor(response);
lock_load = response.spaces.Lock_load;
activeAjaxConnections--;
},
error: function (errorThrown) {
alert(errorThrown);
n }
});
}
}
});
but it give an error xhr is undefined pleas any help and many thanks in advance.
Try flags
Before making ajax call set flag to true and after ajax call is made set flag to false, finally on completion of ajax request again set flag to ture
var ready = true;
$(window).scroll(function(){
if(ready == true){
ready = false;
$.ajax({
url: "/pagination",
cache: false,
success: function (response){
//response
}
}).always(function () {
ready = true; //Reset the flag here
});
}
});
use the below code, use a simple flag variable that will be set to false by the defualt, that is to say that ajax call is not occuring once if condition is met then it will set to true to say that ajax call has started, once the success: or error: call back fires the variable will be set to false so that another ajax call can be made.
startedAjax = false;
if (lock_load === '1') {
startedAjax = true;
var xhr = $.ajax({
type: "POST",
async: true,
dataType: "json",
url: ajaxurl,
data: ({
beforeSend: function (xhr) {
if (activeAjaxConnections != 1) {
xhr.abort();
}
activeAjaxConnections++;
//Show Loader....
$("#Ajax-Load-Image").css('visibility', 'visible');
},
type: "POST",
action: 'Ajax_Get_SpacesAndSponsors',
Page: PageNumber
}),
success: function (response) {
startedAjax = false //set is false
PageNumber++;
var Message = response.spaces.Message;
console.log(response);
console.log(Message);
Draw_SpacesAndSponsor(response);
lock_load = response.spaces.Lock_load;
activeAjaxConnections--;
},
error: function (errorThrown) {
startedAjax = false;
alert(errorThrown);
}
});
}
}
});

ajax Json return type not comming in success

I have following ajax call:
$.ajax({
type: "GET",
url: "/Company/validateForm",
dataType: "json",
data: {
'txtCompanyName': txtCompanyName,
'txtCompanyContactPerson': txtCompanyContactPerson,
'txtCompanyPhone': txtCompanyPhone,
'txtCompanyFax': txtCompanyFax,
'txtCompanyEmail': txtCompanyEmail,
'txtCompanyWebsite': txtCompanyWebsite,
'txtZipcode': txtZipcode,
'txtCountry': txtCountry,
'txtAddress1': txtAddress1,
'txtAddress2': txtAddress2,
'txtCompanyRegNo': txtCompanyRegNo
},
success: function (responceMessage) {
alert(responceMessage);
if (responceMessage != "1") {
alert(responceMessage);
} else {
saveCompanyInformation();
}
},
error: function () {
alert('failure');
}
});
I have made sure that call is going to server side and returning proper message in string format.
But when call from validateForm method on server side is returned, it directly goes to failure instead of success method.
I can't figure out what I'm doing wrong here.
Console is showing:
GET http://localhost:49273/Company/validateForm?txtCompanyName=+x&txtCompanyCon…ebsite=&txtZipcode=&txtCountry=&txtAddress1=&txtAddress2=&txtCompanyRegNo= 500 (Internal Server Error)
I just made cache:false in ajax and code worked.
It was as follows:
$.ajax({
type: "POST",
url: "/Company/validateForm",
cache:false,
dataType: "json",
data:
{
'txtCompanyName': txtCompanyName,
'txtCompanyContactPerson': txtCompanyContactPerson,
'txtCompanyPhone': txtCompanyPhone,
'txtCompanyFax': txtCompanyFax,
'txtCompanyEmail': txtCompanyEmail,
'txtCompanyWebsite': txtCompanyWebsite,
'txtZipcode': txtZipcode,
'txtCountry': txtCountry,
'txtAddress1': txtAddress1,
'txtAddress2': txtAddress2,
'txtCompanyRegNo': txtCompanyRegNo
}
,
success: function (responceMessage) {
if (responceMessage != "0") {
alert(responceMessage);
}
else {
saveCompanyInformation();
}
},
error: function () {
alert('failure');
}
});

Webservice response not coming from $.ajax or $post in javascript

Friends,
i am calling my local webservice & for get & post calls.
for post call when i hit database updated but the response coming from post call is not shown
same happend in case of $ajax 'GET' call firebug console telling '200 OK' but response is empty .
Please tell me whats wrong with my call
$.ajax({
type: 'GET',
url: 'http://localhost/myweb/index.php/quiz/getquiz/8',
dataType: 'json',
error: function(data)
{
alert('Error '+data.error) ;
},
success: function(data)
{
alert('success');
},
complete: function(data)
{
alert('complete track: '+data.status);
},
data: {},
});
I noticed that when error function added it giving message as
function () {
if (list) {
var length = list.length;
add(arguments);
if (firing) {
firingLength = list.length;
} else if (memory && memory !== true) {
firingStart = length;
fire(memory[0], memory[1]);
}
}
return this;
}

Categories

Resources