JQuery Ajax Anonymus Callback Function gets overwritten when called while ajax in progressHow can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?How to manage a redirect request after a jQuery Ajax callWhat's the easiest way to call a function every 5 seconds in jQuery?jQuery: Return data after ajax call successHow to make an AJAX call without jQuery?Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for itJquery Ajax callback not getting executed after geting response from server for JsonpCallback functions not getting calledIs there a callback for when an Angular DTOptionsBuilder ajax call finishes?How to call element.onload inside AJAX sucess function?
Did Michelle Obama have a staff of 23; and Melania have a staff of 4?
Why won't the Republicans use a superdelegate system like the DNC in their nomination process?
What is the proper name for a circle with a line through it?
Heyawake: An Introductory Puzzle
What is the opposite of "hunger level"?
Is this bar slide trick shown on Cheers real or a visual effect?
Why are electric shavers specifically permitted under FAR §91.21
What's a good pattern to calculate a variable only when it is used the first time?
Unconventional examples of mathematical modelling
Is there a word for returning to unpreparedness?
What should I do with the stock I own if I anticipate there will be a recession?
What was the intention with the Commodore 128?
Are there any cons in using rounded corners for bar graphs?
What allows us to use imaginary numbers?
Why does Japan use the same type of AC power outlet as the US?
If a person claims to know anything could it be disproven by saying 'prove that we are not in a simulation'?
Can someone with Extra Attack do a Commander Strike BEFORE he throws a net?
What modifiers are added to the attack and damage rolls of this unique longbow from Waterdeep: Dragon Heist?
Can anybody tell me who this Pokemon is?
What is a "soap"?
Go to last file in vim
What is the most difficult concept to grasp in Calculus 1?
List, map function based on a condition
Bringing Power Supplies on Plane?
JQuery Ajax Anonymus Callback Function gets overwritten when called while ajax in progress
How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?How to manage a redirect request after a jQuery Ajax callWhat's the easiest way to call a function every 5 seconds in jQuery?jQuery: Return data after ajax call successHow to make an AJAX call without jQuery?Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for itJquery Ajax callback not getting executed after geting response from server for JsonpCallback functions not getting calledIs there a callback for when an Angular DTOptionsBuilder ajax call finishes?How to call element.onload inside AJAX sucess function?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a timing or scope problem on this function call.. or no idea what.
AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
AjaxHandlerByClass('url', clientName: this.clientName, function (response) This code gets called 2 times)
From this Function
function AjaxHandlerByClass(className, postData, callback, callbackFail)
var timestamp = new Date().getTime();
var me = this;
me.backHandler = function (data) data.debug)
if (data.debug)
var debug = data.debug;
else if (data.responseJSON && data.responseJSON.debug)
var debug = data.responseJSON.debug;
if (window.console)
for (var key in debug)
if (debug.hasOwnProperty(key))
// console.log(debug[key]);
if (me.mode = 'callback')
callback(data); //<--- this is the bug location
else
callbackFail(data);
;
this.ok = function (data)
me.mode = 'callback';
me.backHandler(data)
this.notOk = function (data)
me.mode = 'callbackFail';
me.backHandler(data)
$.ajax(
contentType: "application/json; charset=utf-8",
url: className + '?ts=' + timestamp + '&sid=' + sid,
type: 'post',
data: JSON.stringify(postData),
dataType: 'json',
cache: false,
success: me.ok,
error: me.notOk
);
The first callback Function never gets executed, while the second one does get executed but 2 times.
The bug happens on the if (me.mode = 'callback') part of the code.
I already tried other options to make the callback function stuck right.
The first attempt was to store the callback function in the Function scope itself.
with assigning it to this.callback and then trying to access it via me.scope
which did not work. then I tried to access the variables directly.. and it is not helping either...
this.callback = callback;
this.callbackFail = callbackFail;
var me = this;
me.backHandler = function (data) ;
I'm on my wit's end.
javascript jquery
add a comment |
I have a timing or scope problem on this function call.. or no idea what.
AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
AjaxHandlerByClass('url', clientName: this.clientName, function (response) This code gets called 2 times)
From this Function
function AjaxHandlerByClass(className, postData, callback, callbackFail)
var timestamp = new Date().getTime();
var me = this;
me.backHandler = function (data) data.debug)
if (data.debug)
var debug = data.debug;
else if (data.responseJSON && data.responseJSON.debug)
var debug = data.responseJSON.debug;
if (window.console)
for (var key in debug)
if (debug.hasOwnProperty(key))
// console.log(debug[key]);
if (me.mode = 'callback')
callback(data); //<--- this is the bug location
else
callbackFail(data);
;
this.ok = function (data)
me.mode = 'callback';
me.backHandler(data)
this.notOk = function (data)
me.mode = 'callbackFail';
me.backHandler(data)
$.ajax(
contentType: "application/json; charset=utf-8",
url: className + '?ts=' + timestamp + '&sid=' + sid,
type: 'post',
data: JSON.stringify(postData),
dataType: 'json',
cache: false,
success: me.ok,
error: me.notOk
);
The first callback Function never gets executed, while the second one does get executed but 2 times.
The bug happens on the if (me.mode = 'callback') part of the code.
I already tried other options to make the callback function stuck right.
The first attempt was to store the callback function in the Function scope itself.
with assigning it to this.callback and then trying to access it via me.scope
which did not work. then I tried to access the variables directly.. and it is not helping either...
this.callback = callback;
this.callbackFail = callbackFail;
var me = this;
me.backHandler = function (data) ;
I'm on my wit's end.
javascript jquery
add a comment |
I have a timing or scope problem on this function call.. or no idea what.
AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
AjaxHandlerByClass('url', clientName: this.clientName, function (response) This code gets called 2 times)
From this Function
function AjaxHandlerByClass(className, postData, callback, callbackFail)
var timestamp = new Date().getTime();
var me = this;
me.backHandler = function (data) data.debug)
if (data.debug)
var debug = data.debug;
else if (data.responseJSON && data.responseJSON.debug)
var debug = data.responseJSON.debug;
if (window.console)
for (var key in debug)
if (debug.hasOwnProperty(key))
// console.log(debug[key]);
if (me.mode = 'callback')
callback(data); //<--- this is the bug location
else
callbackFail(data);
;
this.ok = function (data)
me.mode = 'callback';
me.backHandler(data)
this.notOk = function (data)
me.mode = 'callbackFail';
me.backHandler(data)
$.ajax(
contentType: "application/json; charset=utf-8",
url: className + '?ts=' + timestamp + '&sid=' + sid,
type: 'post',
data: JSON.stringify(postData),
dataType: 'json',
cache: false,
success: me.ok,
error: me.notOk
);
The first callback Function never gets executed, while the second one does get executed but 2 times.
The bug happens on the if (me.mode = 'callback') part of the code.
I already tried other options to make the callback function stuck right.
The first attempt was to store the callback function in the Function scope itself.
with assigning it to this.callback and then trying to access it via me.scope
which did not work. then I tried to access the variables directly.. and it is not helping either...
this.callback = callback;
this.callbackFail = callbackFail;
var me = this;
me.backHandler = function (data) ;
I'm on my wit's end.
javascript jquery
I have a timing or scope problem on this function call.. or no idea what.
AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
AjaxHandlerByClass('url', clientName: this.clientName, function (response) This code gets called 2 times)
From this Function
function AjaxHandlerByClass(className, postData, callback, callbackFail)
var timestamp = new Date().getTime();
var me = this;
me.backHandler = function (data) data.debug)
if (data.debug)
var debug = data.debug;
else if (data.responseJSON && data.responseJSON.debug)
var debug = data.responseJSON.debug;
if (window.console)
for (var key in debug)
if (debug.hasOwnProperty(key))
// console.log(debug[key]);
if (me.mode = 'callback')
callback(data); //<--- this is the bug location
else
callbackFail(data);
;
this.ok = function (data)
me.mode = 'callback';
me.backHandler(data)
this.notOk = function (data)
me.mode = 'callbackFail';
me.backHandler(data)
$.ajax(
contentType: "application/json; charset=utf-8",
url: className + '?ts=' + timestamp + '&sid=' + sid,
type: 'post',
data: JSON.stringify(postData),
dataType: 'json',
cache: false,
success: me.ok,
error: me.notOk
);
The first callback Function never gets executed, while the second one does get executed but 2 times.
The bug happens on the if (me.mode = 'callback') part of the code.
I already tried other options to make the callback function stuck right.
The first attempt was to store the callback function in the Function scope itself.
with assigning it to this.callback and then trying to access it via me.scope
which did not work. then I tried to access the variables directly.. and it is not helping either...
this.callback = callback;
this.callbackFail = callbackFail;
var me = this;
me.backHandler = function (data) ;
I'm on my wit's end.
javascript jquery
javascript jquery
edited Mar 27 at 11:54
Sangoku
asked Mar 27 at 11:49
SangokuSangoku
1,0022 gold badges14 silver badges42 bronze badges
1,0022 gold badges14 silver badges42 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
First of, callBackFail is never defined in your code.
function AjaxHandlerByClass(className, postData, callback, callbackFail)
You pass in three parameters: className, postData and callback.
AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
Second, this line should be me.mode === 'callback', not me.mode = 'callback'
if (me.mode === 'callback')
callback(data);
else
callbackFail(data);
You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass()
to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.
const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);
const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);
Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.
I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.
This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call
– Sangoku
Mar 27 at 16:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55376487%2fjquery-ajax-anonymus-callback-function-gets-overwritten-when-called-while-ajax-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of, callBackFail is never defined in your code.
function AjaxHandlerByClass(className, postData, callback, callbackFail)
You pass in three parameters: className, postData and callback.
AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
Second, this line should be me.mode === 'callback', not me.mode = 'callback'
if (me.mode === 'callback')
callback(data);
else
callbackFail(data);
You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass()
to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.
const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);
const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);
Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.
I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.
This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call
– Sangoku
Mar 27 at 16:31
add a comment |
First of, callBackFail is never defined in your code.
function AjaxHandlerByClass(className, postData, callback, callbackFail)
You pass in three parameters: className, postData and callback.
AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
Second, this line should be me.mode === 'callback', not me.mode = 'callback'
if (me.mode === 'callback')
callback(data);
else
callbackFail(data);
You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass()
to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.
const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);
const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);
Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.
I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.
This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call
– Sangoku
Mar 27 at 16:31
add a comment |
First of, callBackFail is never defined in your code.
function AjaxHandlerByClass(className, postData, callback, callbackFail)
You pass in three parameters: className, postData and callback.
AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
Second, this line should be me.mode === 'callback', not me.mode = 'callback'
if (me.mode === 'callback')
callback(data);
else
callbackFail(data);
You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass()
to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.
const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);
const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);
Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.
I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.
First of, callBackFail is never defined in your code.
function AjaxHandlerByClass(className, postData, callback, callbackFail)
You pass in three parameters: className, postData and callback.
AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
Second, this line should be me.mode === 'callback', not me.mode = 'callback'
if (me.mode === 'callback')
callback(data);
else
callbackFail(data);
You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass()
to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.
const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);
const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);
Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.
I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.
answered Mar 27 at 16:16
henrik123henrik123
6085 silver badges12 bronze badges
6085 silver badges12 bronze badges
This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call
– Sangoku
Mar 27 at 16:31
add a comment |
This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call
– Sangoku
Mar 27 at 16:31
This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call
– Sangoku
Mar 27 at 16:31
This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call
– Sangoku
Mar 27 at 16:31
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55376487%2fjquery-ajax-anonymus-callback-function-gets-overwritten-when-called-while-ajax-i%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown