This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I know this has been asked a million times, but all I see is people explaining scope and no one explains a solution.
In this case HOW do I get alert to show the value of comment_count rather than undefined.
var comment_count;
$.post( "<?=site_url('comments/ajax_get_comment_count');?>",{tags:[aData[7],'BWQ']}, function( data ) {
comment_count = data;
});
alert(comment_count);
I think you need something like that:
var comment_count;
function controller(){
return {
setValue:function(_val,data){
_val = data;
this.fireValue(_val)
},
fireValue:function(_val){
alert(_val)
}
}
}
$.post("<?=site_url('comments/ajax_get_comment_count');?>", {tags: [aData[7], 'BWQ']}, function (data) {
controller.setValue(comment_count,data)
});
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
So I have this javascript code. There are 2 nested functions. How can I return a value to funcA() inside funcB() ?
function funcA() {
funcB(() => {
//return funcA inside here
})
}
Is this even possible without doing it like the following?
function funcA() {
let returnValueA;
funcB(() => {
//change returnValueA inside here
})
return returnValueA;
}
In short: No.
A return statement only defines what is returned from the function it is part of.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I want to return a ajax call value to other function. Code shown below
script
var obj2 = ajax_call();
function ajax_call() {
var obj3;
$.post("action.php",{action: 'view_style_code_action',style_val: v},function(data,status){
obj3 = $.parseJSON(data);
});
return(obj3);
}
console.log(obj2+'test');
function ajax_call() {
return $.post("action.php",{action: 'view_style_code_action',style_val: v},function(data,status){
return data;
});
}
var obj2 = ajax_call();
console.log(obj2+'test');
AJAX returns result in future (that's why it is asynchronous). Since JavaScript runs in a single thread, you cannot return a value immediately.
You need to understand jQuery's Promise API instead, which $.post already supports.
Modify your code a bit to leverage the power of promises in your code.
function ajax_call() {
return $.post("action.php", {action: 'view_style_code_action',style_val: v})
}
ajax_call()
.then(function(data) {
// Do the parsing here
var obj3 = $.parseJSON(data);
// Do rest of your work here.
...
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
How I can return a string from a javascript callback
I have two functions, main function is working on loaded.
and another function is used to calling web service.
I would like to know how can JS return the string value to main function.
thanks
function thisCallJSON(webServiceURL) {
var params = {};
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
gadgets.io.makeRequest(webServiceURL, function(response)
{
if(response.data && response.text)
{
var jsondata = response.data;
for (var key in jsondata)
{
var value = jsondata[key];
//alert("Member Name : "+value["memNm"]);
}
}
else
{
//alert("Member Name : Not Found !");
}
}, params);
}; function main(){
var txt_string = "";
txt_string = thisCallJSON("http://192.100.1.59");
}
You can assign the value to the variable in the scope of the main function, but it won't happen before the main function is finished executing because of the event loop. Instead, you should put your code inside the callback, or better yet, look at how you would use javascript promises to accomplish this.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I'm trying to append the results from an ajax call to a paragraph using jQuery.
I would like to return the variable "myResult" from the inner getResult function and pass it to the outer buildParagraph function, but the value returned is undefined.
How do I append the value of myResults to the <p> tag as indicated below?
function buildParagraph () {
function getResult(url) {
$.getJSON(url, function(data) {
var myResult = data.results;
return myResult;
}
}
var myUrl = 'www.mywebsite.com';
getResult(myUrl);
$('<p>').html(myResult);
}
You need to have a callback function inside ajax success,Or the easiest way is move the below code to the ajax success function
$('<p>').html(data.results);
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
How to return AJAX response Text? [duplicate]
(2 answers)
Closed 9 years ago.
$(document).ready(function(){
// Global function (will be include in any HTML file)
function m3_result(size_1, size_2, size_3){
$.get('http://www.google.com', function(data){
return data;
});
}
// Another function
function calculate(){
var size_1 = parseFloat($('#add_document_form #size_1').val());
var size_2 = parseFloat($('#add_document_form #size_2').val());
var size_3 = parseFloat($('#add_document_form #size_3').val());
var ax = m3_result(size_1, size_2, size_3);
alert(ax); // Here ax return: UNDEFINED
}
// Run
calculate();
});
Results are "undefined", but I would like that calculate() will wait for m3_result() to execute. I see that here problem is coming when I added $.get(), but its needed...
I have searching for callback() like functions, but none fit to my needs, or I just didnt put that right.
Any help will be highly appreciated, thanks.
GET url will be localy and element IDs are also ok.
You can't return a result from an asynchronous function, instead you can return a promise to supply that value later, which as it happens is the jqXHR object returned by $.get:
function m3_result() {
return $.get(...)
}
and do the same in calculate:
function calculate() {
...
return m3_result(...);
}
and then wait for the result, which will be passed as a parameter to the callback registered with the .done function:
calculate().done(function(result) {
alert(result);
});