How can I do: var value = foo(bar); Where foo a function that looks up the value through a JSON request
Coming from a PHP background I run into this kind of problem often when I'm programming with JavaScript & jQuery. I need a function that needs to take a variable and do a server lookup to get a status result back.
Rather than having to repeat the same code (which I am doing now and my boss dislikes) in 4 different places, I would prefer it to be a function I can call from anywhere.
The actual code is a lot more complex, I stripped it down to the basic concept for the purposes of this question.
I tried using "async: false" so that the function would wait for the ajax call to complete before returning, but it didn't work.
The solution likely is to use a Callback function. How exactly does this work and how would I need to structure my code to achieve this universally in different locations of my app (code)?
var sample = "ABC12345";
var samplestatus = getStatus(sample);
console.log("!!!!" + samplestatus + "!!!!");
function getStatus(samplenumber)
var jsonurl = "lookup.php?s="+samplenumber;
$.ajax(
url: jsonurl,
dataType: 'json',
async: false,
success: function(data1)
var s_status;
if (typeof(data1[0]) !== 'undefined')
var data = data1[0];
s_status = data.STATUS;
console.log("Sample " + samplenumber + " has a status of "+s_status);
return(s_status);
else
return(0);
);
The output:
Sample ABC12345 has a status of COMPLETED
!!!!undefined!!!!
jquery ajax function
add a comment |
Coming from a PHP background I run into this kind of problem often when I'm programming with JavaScript & jQuery. I need a function that needs to take a variable and do a server lookup to get a status result back.
Rather than having to repeat the same code (which I am doing now and my boss dislikes) in 4 different places, I would prefer it to be a function I can call from anywhere.
The actual code is a lot more complex, I stripped it down to the basic concept for the purposes of this question.
I tried using "async: false" so that the function would wait for the ajax call to complete before returning, but it didn't work.
The solution likely is to use a Callback function. How exactly does this work and how would I need to structure my code to achieve this universally in different locations of my app (code)?
var sample = "ABC12345";
var samplestatus = getStatus(sample);
console.log("!!!!" + samplestatus + "!!!!");
function getStatus(samplenumber)
var jsonurl = "lookup.php?s="+samplenumber;
$.ajax(
url: jsonurl,
dataType: 'json',
async: false,
success: function(data1)
var s_status;
if (typeof(data1[0]) !== 'undefined')
var data = data1[0];
s_status = data.STATUS;
console.log("Sample " + samplenumber + " has a status of "+s_status);
return(s_status);
else
return(0);
);
The output:
Sample ABC12345 has a status of COMPLETED
!!!!undefined!!!!
jquery ajax function
1
You need to return the ajax request. The issue is that the function itself isnt returning anything. it is executing code therein. So you would want to look at Promises, and develop a promise, and then await it.
– Fallenreaper
Nov 15 '18 at 1:31
@Fallenreaper thank you. Looks like Ken Yoro Ko did something similar below.
– Michael Fever
Nov 16 '18 at 20:48
add a comment |
Coming from a PHP background I run into this kind of problem often when I'm programming with JavaScript & jQuery. I need a function that needs to take a variable and do a server lookup to get a status result back.
Rather than having to repeat the same code (which I am doing now and my boss dislikes) in 4 different places, I would prefer it to be a function I can call from anywhere.
The actual code is a lot more complex, I stripped it down to the basic concept for the purposes of this question.
I tried using "async: false" so that the function would wait for the ajax call to complete before returning, but it didn't work.
The solution likely is to use a Callback function. How exactly does this work and how would I need to structure my code to achieve this universally in different locations of my app (code)?
var sample = "ABC12345";
var samplestatus = getStatus(sample);
console.log("!!!!" + samplestatus + "!!!!");
function getStatus(samplenumber)
var jsonurl = "lookup.php?s="+samplenumber;
$.ajax(
url: jsonurl,
dataType: 'json',
async: false,
success: function(data1)
var s_status;
if (typeof(data1[0]) !== 'undefined')
var data = data1[0];
s_status = data.STATUS;
console.log("Sample " + samplenumber + " has a status of "+s_status);
return(s_status);
else
return(0);
);
The output:
Sample ABC12345 has a status of COMPLETED
!!!!undefined!!!!
jquery ajax function
Coming from a PHP background I run into this kind of problem often when I'm programming with JavaScript & jQuery. I need a function that needs to take a variable and do a server lookup to get a status result back.
Rather than having to repeat the same code (which I am doing now and my boss dislikes) in 4 different places, I would prefer it to be a function I can call from anywhere.
The actual code is a lot more complex, I stripped it down to the basic concept for the purposes of this question.
I tried using "async: false" so that the function would wait for the ajax call to complete before returning, but it didn't work.
The solution likely is to use a Callback function. How exactly does this work and how would I need to structure my code to achieve this universally in different locations of my app (code)?
var sample = "ABC12345";
var samplestatus = getStatus(sample);
console.log("!!!!" + samplestatus + "!!!!");
function getStatus(samplenumber)
var jsonurl = "lookup.php?s="+samplenumber;
$.ajax(
url: jsonurl,
dataType: 'json',
async: false,
success: function(data1)
var s_status;
if (typeof(data1[0]) !== 'undefined')
var data = data1[0];
s_status = data.STATUS;
console.log("Sample " + samplenumber + " has a status of "+s_status);
return(s_status);
else
return(0);
);
The output:
Sample ABC12345 has a status of COMPLETED
!!!!undefined!!!!
jquery ajax function
jquery ajax function
asked Nov 15 '18 at 1:21
Michael FeverMichael Fever
4192416
4192416
1
You need to return the ajax request. The issue is that the function itself isnt returning anything. it is executing code therein. So you would want to look at Promises, and develop a promise, and then await it.
– Fallenreaper
Nov 15 '18 at 1:31
@Fallenreaper thank you. Looks like Ken Yoro Ko did something similar below.
– Michael Fever
Nov 16 '18 at 20:48
add a comment |
1
You need to return the ajax request. The issue is that the function itself isnt returning anything. it is executing code therein. So you would want to look at Promises, and develop a promise, and then await it.
– Fallenreaper
Nov 15 '18 at 1:31
@Fallenreaper thank you. Looks like Ken Yoro Ko did something similar below.
– Michael Fever
Nov 16 '18 at 20:48
1
1
You need to return the ajax request. The issue is that the function itself isnt returning anything. it is executing code therein. So you would want to look at Promises, and develop a promise, and then await it.
– Fallenreaper
Nov 15 '18 at 1:31
You need to return the ajax request. The issue is that the function itself isnt returning anything. it is executing code therein. So you would want to look at Promises, and develop a promise, and then await it.
– Fallenreaper
Nov 15 '18 at 1:31
@Fallenreaper thank you. Looks like Ken Yoro Ko did something similar below.
– Michael Fever
Nov 16 '18 at 20:48
@Fallenreaper thank you. Looks like Ken Yoro Ko did something similar below.
– Michael Fever
Nov 16 '18 at 20:48
add a comment |
1 Answer
1
active
oldest
votes
You should return from the function you call.
function getStatus(samplenumber)
var jsonurl ="lookup.php?s="+samplenumber;
return $.ajax(
url: jsonurl,
dataType: 'json',
async: false,
success: function(data1)
var s_status;
if (typeof(data1[0]) !== 'undefined')
var data = data1[0];
s_status = data.STATUS;
console.log("Sample " + samplenumber + " has a status of "+s_status);
return s_status;
else
return 0;
);
This works, but one thing I noticed is you still have the 2 returns in there, return s_status and return 0. If you're returning the whole thing are those 2 calls still required?
– Michael Fever
Nov 16 '18 at 20:46
1
@MichaelFever Success is just a function call, just like error, and done. (look up AJAX Requests). As long as the success call is fired, that function executes, and will do whatever you want, just like any other function. His is doing some status validation.
– Fallenreaper
Nov 17 '18 at 14:37
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%2f53311130%2fhow-can-i-do-var-value-foobar-where-foo-a-function-that-looks-up-the-value%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
You should return from the function you call.
function getStatus(samplenumber)
var jsonurl ="lookup.php?s="+samplenumber;
return $.ajax(
url: jsonurl,
dataType: 'json',
async: false,
success: function(data1)
var s_status;
if (typeof(data1[0]) !== 'undefined')
var data = data1[0];
s_status = data.STATUS;
console.log("Sample " + samplenumber + " has a status of "+s_status);
return s_status;
else
return 0;
);
This works, but one thing I noticed is you still have the 2 returns in there, return s_status and return 0. If you're returning the whole thing are those 2 calls still required?
– Michael Fever
Nov 16 '18 at 20:46
1
@MichaelFever Success is just a function call, just like error, and done. (look up AJAX Requests). As long as the success call is fired, that function executes, and will do whatever you want, just like any other function. His is doing some status validation.
– Fallenreaper
Nov 17 '18 at 14:37
add a comment |
You should return from the function you call.
function getStatus(samplenumber)
var jsonurl ="lookup.php?s="+samplenumber;
return $.ajax(
url: jsonurl,
dataType: 'json',
async: false,
success: function(data1)
var s_status;
if (typeof(data1[0]) !== 'undefined')
var data = data1[0];
s_status = data.STATUS;
console.log("Sample " + samplenumber + " has a status of "+s_status);
return s_status;
else
return 0;
);
This works, but one thing I noticed is you still have the 2 returns in there, return s_status and return 0. If you're returning the whole thing are those 2 calls still required?
– Michael Fever
Nov 16 '18 at 20:46
1
@MichaelFever Success is just a function call, just like error, and done. (look up AJAX Requests). As long as the success call is fired, that function executes, and will do whatever you want, just like any other function. His is doing some status validation.
– Fallenreaper
Nov 17 '18 at 14:37
add a comment |
You should return from the function you call.
function getStatus(samplenumber)
var jsonurl ="lookup.php?s="+samplenumber;
return $.ajax(
url: jsonurl,
dataType: 'json',
async: false,
success: function(data1)
var s_status;
if (typeof(data1[0]) !== 'undefined')
var data = data1[0];
s_status = data.STATUS;
console.log("Sample " + samplenumber + " has a status of "+s_status);
return s_status;
else
return 0;
);
You should return from the function you call.
function getStatus(samplenumber)
var jsonurl ="lookup.php?s="+samplenumber;
return $.ajax(
url: jsonurl,
dataType: 'json',
async: false,
success: function(data1)
var s_status;
if (typeof(data1[0]) !== 'undefined')
var data = data1[0];
s_status = data.STATUS;
console.log("Sample " + samplenumber + " has a status of "+s_status);
return s_status;
else
return 0;
);
answered Nov 15 '18 at 1:40
Ken Yoro KoKen Yoro Ko
355
355
This works, but one thing I noticed is you still have the 2 returns in there, return s_status and return 0. If you're returning the whole thing are those 2 calls still required?
– Michael Fever
Nov 16 '18 at 20:46
1
@MichaelFever Success is just a function call, just like error, and done. (look up AJAX Requests). As long as the success call is fired, that function executes, and will do whatever you want, just like any other function. His is doing some status validation.
– Fallenreaper
Nov 17 '18 at 14:37
add a comment |
This works, but one thing I noticed is you still have the 2 returns in there, return s_status and return 0. If you're returning the whole thing are those 2 calls still required?
– Michael Fever
Nov 16 '18 at 20:46
1
@MichaelFever Success is just a function call, just like error, and done. (look up AJAX Requests). As long as the success call is fired, that function executes, and will do whatever you want, just like any other function. His is doing some status validation.
– Fallenreaper
Nov 17 '18 at 14:37
This works, but one thing I noticed is you still have the 2 returns in there, return s_status and return 0. If you're returning the whole thing are those 2 calls still required?
– Michael Fever
Nov 16 '18 at 20:46
This works, but one thing I noticed is you still have the 2 returns in there, return s_status and return 0. If you're returning the whole thing are those 2 calls still required?
– Michael Fever
Nov 16 '18 at 20:46
1
1
@MichaelFever Success is just a function call, just like error, and done. (look up AJAX Requests). As long as the success call is fired, that function executes, and will do whatever you want, just like any other function. His is doing some status validation.
– Fallenreaper
Nov 17 '18 at 14:37
@MichaelFever Success is just a function call, just like error, and done. (look up AJAX Requests). As long as the success call is fired, that function executes, and will do whatever you want, just like any other function. His is doing some status validation.
– Fallenreaper
Nov 17 '18 at 14:37
add a comment |
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%2f53311130%2fhow-can-i-do-var-value-foobar-where-foo-a-function-that-looks-up-the-value%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
1
You need to return the ajax request. The issue is that the function itself isnt returning anything. it is executing code therein. So you would want to look at Promises, and develop a promise, and then await it.
– Fallenreaper
Nov 15 '18 at 1:31
@Fallenreaper thank you. Looks like Ken Yoro Ko did something similar below.
– Michael Fever
Nov 16 '18 at 20:48