in my jquery function i have two ajax call with serverside method and its working fine,
problem is ajax call at last amd after ajax line of code run but this lines of code depand on
function Rbook(b) {
var one = $(b).attr("data-oneislcc");
var two = $(b).attr("data-twoislcc");
var trip1 = $(b).attr("data-oneinfo");
var trip2 = $(b).attr("data-twoinfo");
var owflt = "l";
var inflt = 'r';
var owjdata = $(b).attr("data-ow");
var iwjdata = $(b).attr("data-iw");
var llccreturn, rlccreturn;
var lres, rres;
$("#fadeing").css("display", "block");
$("#fade").css("display", "block").css("height", $(document).height / 2);
if (one == 'true') {
$.ajax({
type: "POST",
url: "Search-RoundResult.aspx/FareQuoteMethod",
data: "{'ALcode':'" + trip1 + "','flt':'" + owflt + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnSuccess(response) {
if (response.d != null) {
lres = response.d;
if (response.d == "sessionExpire") {
}
else {
var data = new Array()
data = response.d.split("oldfare=");
llccreturn = owlcc(data[0], data[1])
}
}
else {
alert("Please Try agian.");
}
},
Error: function errer(msg) {
$("#fade").css("display", "none");
$("#light").css("display", "none");
alert(msg.d)
}
});
}
else {
llccreturn = ownonlcc(owjdata);
}
if (two == 'true') {
$.ajax({
type: "POST",
url: "Search-RoundResult.aspx/FareQuoteMethod",
data: "{'ALcode':'" + trip2 + "','flt':'" + inflt + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnSuccess(response) {
if (response.d != null) {
if (rres == "sessionExpire") {
}
else {
var data = new Array()
data = response.d.split("oldfare=");
rlccreturn = iwlcc(data[0], data[1])
}
}
else {
alert("Please Try agian.");
}
},
Error: function errer(msg) {
$("#fade").css("display", "none");
$("#light").css("display", "none");
alert(msg.d)
}
});
}
else {
rlccreturn = iwnonlcc(iwjdata);
}
if (llccreturn == 'farechange' || rlccreturn == 'farechange') {
$("#farechange").css("display", "block");
$("#fade").css("display", "block");
}
if (llccreturn == 'nofarechange' || rlccreturn == 'nofarechange') {
window.location = "reviewbooking.aspx?trip1=" + $(b).attr("data-oneinfo") + "&iwlcc=" + $(b).attr("data-oneislcc") + "&trip2=" + $(b).attr("data-twoinfo") + "&owlcc=" + $(b).attr("data-twoislcc");
}}
var owlcc = function (jdata, oldfare) {
//Some Calulation
retrun 'farechange';
}
var ownonlcc = function (jdata) { //Some Calulation
retrun 'nofarechange'}
var iwlcc = function (jdata, oldfare) { //Some Calulation
return 'farechange'}
var iwnonlcc = function (jdata) { retrun 'nofarechange'}
if run this code its run this line of code then rest so condition not validate
i dont know where i m doing wrong
if (llccreturn == 'farechange' || rlccreturn == 'farechange') {
$("#farechange").css("display", "block");
$("#fade").css("display", "block");
}
if (llccreturn == 'nofarechange' || rlccreturn == 'nofarechange') {
window.location = "reviewbooking.aspx?trip1=" + $(b).attr("data-oneinfo") + "&iwlcc=" + $(b).attr("data-oneislcc") + "&trip2=" + $(b).attr("data-twoinfo") + "&owlcc=" + $(b).attr("data-twoislcc");
}
It looks like you don't understand asynchronous javascript. When you do an ajax call, it sends the request, then continues running the code and only later runs the success handler. Otherwise, it wouldn't be able to do anything at all until the response came back, since javascript is single-threaded.
The Rbook function does the following: First, get lots of information from the DOM; then, send some ajax requests (and set handlers to run when the response comes back); then possibly make some changes to the DOM, depending on the values of llccreturn and rlccreturn (but they're still undefined). It's only when the ajax response comes back and the success handler is run that these variables are set, but by then it's too late.
If you want to run some code after hearing the ajax response, put it in the success handler (or call it from the success handler). In this case, it's further complicated by the fact that you don't want to run the code until both ajax responses have arrived, and also by the fact that you might just run iwnonlcc or ownonlcc synchronously instead of doing an ajax call. I'd say the easiest way to fix this would be to wrap the code you want to run at the end inside a function and an if block like this:
function dataWasReceived() {
if (llccreturn !== undefined && rlccreturn !== undefined) {
if (llccreturn == 'farechange' || rlccreturn == 'farechange') {
$("#farechange").css("display", "block");
$("#fade").css("display", "block");
}
if (llccreturn == 'nofarechange' || rlccreturn == 'nofarechange') {
window.location = "reviewbooking.aspx?trip1=" + $(b).attr("data-oneinfo") + "&iwlcc=" + $(b).attr("data-oneislcc") + "&trip2=" + $(b).attr("data-twoinfo") + "&owlcc=" + $(b).attr("data-twoislcc");
}
}
}
Then, every time you set the value of llccreturn or rlccreturn, call this function:
rlccreturn = iwlcc(data[0], data[1])
dataWasReceived();
and:
rlccreturn = iwnonlcc(iwjdata);
dataWasReceived();
etc.
I'm also concerned about this line (appears twice):
data: "{'ALcode':'" + trip2 + "','flt':'" + inflt + "'}",
You probably wanted to apply the argument as an object, not a JSON string representing that object:
data: {ALcode: trip2, flt: inflt},
(Incidentally, the string you gave wasn't valid JSON anyway, since it used ' instead of ".)
Related
I have a ajax-call to a script for searching numbers. The response is a json array with name and surname(Strings). The client-script is this and I think really don't see why the script is looping and sending the response multiple times. The toogle-solution was the last thing I tried.
$(document).ready(function () {
$("#phone").keyup(function () {
var number = $(this).val();
var toogle = 0;
if (number.length == 10 && toogle == 0) {
alert('inside with 10 numbers');
toogle = 1;
$.ajax({
type: "POST",
url: "info-phone.php",
dataType: "jsonp",
data: {
number: number
}
}).done(function (msg) {
toogle = 0;
if (msg.Name != "" && msg.Surname != "") {
$("#phone").add("Are you " + msg.Name + " " + msg.Surname);
};
}); //done-function
}
}); //phone-keyup
}); //document-ready
Basically I have a input, and when the user reaches 10 numbers this script will call the server and get the name to that number.
Any ideas? Just a typo?
try this:
$(document).ready(function () {
window.toogle = 0;
$("#phone").click(function () {
var number = $(this).val();
if (number.length == 10 && window.toogle == 0) {
alert('inside with 10 numbers');
window.toogle = 1;
$.ajax({
type: "POST",
url: "info-phone.php",
dataType: "jsonp",
data: {
number: number
}
}).done(function (msg) {
window.toogle = 0;
if (msg.Name != "" && msg.Surname != "") {
$("#phone").add("Are you " + msg.Name + " " + msg.Surname);
}
}); //done-function
}
}); //phone-keyup
}); //document-ready
I think your issue is that you're using .length which measures the length of strings, on the return of the jQuery .val() method, which returns strings or "numbers". As your input is a phone number, I think the .val() method is returning an integer, and you'd need to convert it to a string for .length to work correctly.
Try
number.toString().length;
First of all, you should not write anonymous functions with several identations.
Just name your functions to see clearer in that mess!
Your variables number and toogle are local to the anonymous function you call doing keyup.
I think there might be a problem here no?
Like this:
$(document).ready(function () {
window.toogle = 0;
$("#phone").click(phoneKeyUp); //phone-keyup -> this one you declare it to make document.ready() clearer
}); //document-ready
var phoneKeyUp = function() {
var number = $(this).val();
if (number.length == 10 && window.toogle == 0) {
alert('inside with 10 numbers');
window.toogle = 1;
$.ajax({
type: "POST",
url: "info-phone.php",
dataType: "jsonp",
data: {
number: number
}
}).done(function (msg) {
window.toogle = 0;
if (msg.Name != "" && msg.Surname != "") {
$("#phone").add("Are you " + msg.Name + " " + msg.Surname);
}
}); //done-function -> this one may stay here
}
};
Have a requirement to call the confirm box in JSP, following is my code in controller,
if(!(nesting)){
bla.add("Do you want to Load anyway?");
context.getFlowScope().put("bla", bla);
context.getFlowScope().put("uldSelector", uldSelector);
return;
}
h = uloService.processDomBatch(histInfo, dl, items);
the above will add the error to flowscope and returns, but, I wantto call a confirm box instead and then depending yes/no, I should continue through...
is there a way to do this?..any help is greatly appreciated!
Thank you!
Yes #user1609085 you can do it with AJAX and JavaScript something like this:
function question(val) {
var chk = document.forms[0].chk
var box = valBox(chk)
var resp = confirm("Do you want to Load anyway?")
if(resp) {
if(box == null) { return }
else {
$.ajax({
type: 'GET',
url: 'controllerMethod?action=controllerMethod',
data: 'uldSelector='+val,
cache: false,
success: function(data) {
alert("histInfo")
},
error: function(data) {
alert('ERROR: ' + data)
}
})
}
}
}
function valBox(b) {
var cnt = -1
for(var i = b.length-1; i > -1; i--) {
if(b[i].checked) {
cnt = i
i = -1
}
}
if(cnt > -1) return b[cnt].value
else return null
}
And you box only put onclick="question(this.value)"
I hope help you :)
function DoInsert(ind) {
var sourceIndex = $("#lstAvailableCode").val();
var targetIndex = $("#lstCodelist").val();
var success = 0;
var rightSelectedIndex = $("#lstCodelist").get(0).selectedIndex;
var functionName = "/Ajax/SaveCodeforInsert";
if (ind == "plan") {
functionName = "/Ajax/SaveCodeforInsertForPlan";
}
$.ajax({
type: "POST",
traditional: true,
url: functionName,
async: false,
data: "ControlPlanNum=" + $("#ddControlPlan").val() + "&LevelNum=" + $("#ddlLevel").val() + "&ColumnNum=" + $("#ddlColumn").val() + "&SourcbaObjectID=" + sourceIndex + "&TargetbaObjectID=" + targetIndex + "&userID=<%=Model.userID%>",
dataType: "json",
error: function (data) {
alert("Error Adding Code");
FinishAjaxLoading();
},
success: function (data) {
if (data == 0) { success = 1; } else { success = data; }
FinishAjaxLoading();
var x = $("#lstAvailableCode").val();
$("#lstCodelist").val(x);
$("#lstCodelist").val(x).css("background-color", "#ffffff");
}
});
Here I am trying to adding one item from lstAvailableCode list box to lstCodelist box. after adding into lstCodelist box I am trying to change the textcolor to yellow or some other color.
on my success message i wrote something like this. But I am not able to change the color of the text even I am not able to change the backgroud color of that list box. is that something I am doing wrong here?
here is my lstCodelist box code.
<select id="lstCodelist" size="17" name="lstCodelist" style="width:100%;height:280px;background-color:#EFEFFB;"></select>
$.fn.fillSelectDD = function (data) {
return this.clearSelectDD().each(function () {
if (this.tagName == 'SELECT') {
var dropdownList = this;
$.each(data, function (index, optionData) {
var option = new Option(optionData.Text, optionData.Value);
if ($.browser.msie) {
dropdownList.add(option);
}
else {
dropdownList.add(option, null);
}
});
}
});
}
$("#lstCodelist").val(x).css("background-color", "#ffffff");
should be
$("#lstCodelist").css("background-color", "#ffffff");
.val() returns a value, not the original jQuery object.
To change the font color, you wold use:
$("#lstCodelist").css("color", "#00ffff");
$("#lstCodelist").val(x).css("background-color", "#ffffff");
should be
$("#lstCodelist").css("background-color", "#ffffff");
Otherwise you're trying to set a css property on whatever string/number the .val() call returns, and NOT on the actual page element that the value's coming from.
I have been struck up in a condition where i need to halt the Confirm box. This is what I have done:
function onBeforeClientInsert(record) {
var eventtype = parseInt(record. < %= CEO.FieldEvaluator.GetEvaluatorByDId("EVENT_TYPE_ID").GetFieldDataFieldId() % > );
var begindate = record. < %= CEO.FieldEvaluator.GetEvaluatorByDId("BeginDate").GetFieldDataFieldId() % > ;
var enddate = record. < %= CEO.FieldEvaluator.GetEvaluatorByDId("EndDate").GetFieldDataFieldId() % > ;
$.ajax({
type: "POST",
url: "Data.aspx/CheckInsertRecord",
data: "{EventType:'" + eventtype + "',BeginDate:'" + begindate + "'," + "EndDate:'" + enddate + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
if (msg.d == "No duplicate") {
} else {
alert(msg.d);
eval("var data = " + msg.d + ";");
}
var i = 0;
alert(i);
alert(data[i]);
do {
$("#beginDate").html(data[i].BeginDate);
$("#eventTypeID").html(data[i].EVENT_TYPE_ID);
$("#endDate").html(data[i].EndDate);
$("#beginlatlong").html(data[i].BeginLATLONG);
$("#endlatlong").html(data[i].EndLATLONG);
var modal = document.getElementById('Div1');
modal.style.display = '';
modal.style.position = 'fixed';
modal.style.zIndex = '100';
modal.style.left = '30%';
modal.style.top = '10%';
var screen = document.getElementById('modalScreen');
screen.style.display = '';
i++;
if (confirm("Are you sure you want to continue?") == false) {
hide();
continue;
}
}
while (msg.d != null);
}
});
if (confirm("Are you sure you want to insert this new record ?") == false) {
hide();
return false;
}
if (Page_ClientValidate("<%= CEO.GridUtils.Global_ValidationGroupName%>")) {
hide();
SetPostBackCause('INSERT');
return true;
}
return false;
}
So,the problem has been that
if (confirm("Are you sure you want to insert this new record ?") == false) {
hide();
return false;
}
would be run immediately after the confirm box
if(confirm("Are you sure you want to continue?")==false){
hide();
continue;
}
But i want it to be halted until the user clicks something on the first confirm box. Can u please let me know how to do this? Also Can u let me know any other way to do this, if I'm approaching it in wrong way?
Ajax is asynchronous.
You need to move all dependent code into the success callback.
success: function (msg) {
// snip ...
if (confirm("Are you sure you want to insert this new record ?") == false) {
hide();
}
if (Page_ClientValidate("<%= CEO.GridUtils.Global_ValidationGroupName%>")) {
hide();
SetPostBackCause('INSERT');
}
}
You could use async: false to make the request sychronous but I do not recommend this.
You need to place all of this:
if (confirm("Are you sure you want to insert this new record ?") == false) {
hide();
return false;
}
if (Page_ClientValidate("<%= CEO.GridUtils.Global_ValidationGroupName%>")) {
hide();
SetPostBackCause('INSERT');
return true;
}
within your AJAX callback if you want it to run after the first dialog. Currently, you set the callback, and then call the code above. If your internet connection was really slow, the first dialog you are seeing could theoretically come after the second you are seeing.
Have you considered the async: false option to $.ajax?
When i execute it in firefox mozila than this code is working well (in case of register user) but when we try it in IE (iternet explorer 8) then alert("sorry u must have to login first"); this message is comming. ( in both cases as register or gest).
Another thing: for gest user returning data from server is null. means d = null,
Another thing when execute in firefox mozila as a gest user then nothing happen means alert("sorry u must have to login first"); this message is not comming.
What should i do?
function manageVoting() {
var parameter;
var myVoting;
var divVoting;
var divVotes;
var value = -1;
var parameterData;
$('div.votemaincontainer').each(function() {
parameter = $(this).find('#[id$= hfUrl]').val();
myVoting = parseInt($(this).find('#[id$=hfMyVote]').val());
divVoting = $(this).find('[id$=divVoting]');
divVotes = $(this).find('[id$=divVotes]');
function processVote(value) {
if (value == 0 || value == 1) {
parameterData = parameter + value + "'}";
$.ajax({
type: 'POST',
url: 'UserControls/Vote/VoteAction.aspx/Voting',
data: parameterData,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
var result = eval(data.d);
if (result) {
if (result.length > 1) {
if (result[1] == 1 && result[2] == 1) {
$('img.voteupImage').attr('src', 'UserControls/Vote/Images/aftervote_arrow_up.png');
$('img.votedownImage').attr('src', 'UserControls/Vote/Images/arrow_down.png');
$('div.divVotes').html(result[0]);
$(myVoting).val(value);
}
else if (result[1] == 0 && result[2] == 1) {
$('img.voteupImage').attr('src', 'UserControls/Vote/Images/Arrow Up.png');
$('img.votedownImage').attr('src', 'UserControls/Vote/Images/aftervote_down.png');
$('div.divVotes').html(result[0]);
$(myVoting).val(value);
}
else if (result[2] < 0 && value == 0) {
alert('U HAVE ALL READY VOTED DOWN');
}
else {
alert('U HAVE ALL READY VOTED UP');
}
$('#[id$=hfMyVote]').html(result[1]);
}
else {
alert('I AM ENSIDE ELSE');
//$('div.divVotes').html(result[0] - 1);
alertDialog("Rating any knowledge item is only available for Registered User.<br>Do you want to <a class='signUpPopUp' href='signup.aspx'> signup</a> Now?");
}
}
},
error: function() {
alert("sorry u must have to login first");
}
});
}
}
$('img.voteupImage').live('click', function() {
value = 1;
processVote(value);
});
$('img.votedownImage').live('click', function() {
value = 0;
processVote(value);
});
});
}
$(function() {
manageVoting();
});
For the ajax call to be successful or not does not depend on the user being authenticated. The http server should return a 403 code if the user is not authenticated and 200 if everything is ok.
success(data, textStatus, XMLHttpRequest){
if (XMLHttpRequest.status == 403){
alert("sorry u must have to login first");
return;
}
}