How can I do: var value = foo(bar); Where foo a function that looks up the value through a JSON request










0















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!!!!









share|improve this question

















  • 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















0















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!!!!









share|improve this question

















  • 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













0












0








0


1






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!!!!









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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












1 Answer
1






active

oldest

votes


















1














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;


);






share|improve this answer























  • 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











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
);



);













draft saved

draft discarded


















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









1














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;


);






share|improve this answer























  • 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
















1














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;


);






share|improve this answer























  • 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














1












1








1







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;


);






share|improve this answer













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;


);







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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




















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto