This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I have a global object declared at first line. Then filling the data into it using jquery getJSON. The issue here is I am getting empty object inside function slider, whereas inside getJSON it is printing proper data. Any idea whats wrong here?
var allslides = {};
$.getJSON("data/slides.json", function(data) {
$.each(data, function(key, val) {
allslides[key] = { image : val.image, title: val.title, desc:val.desc };
});
console.log(allslides); // First
});
$(function(){
slider();
});
function slider() {
console.log(allslides); // second
}
Move your call to slider into your .getJSON as it is asynchronous:
$.getJSON("data/slides.json", function(data) {
$.each(data, function(key, val) {
allslides[key] = { image : val.image, title: val.title, desc:val.desc };
});
slider();
});
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
I have two functions country and location. When the page loads these two functions will run.
I need to get the result value from $.getScript and use it in the function location().
Currently, I am getting undefined. Please check my code.
Any help would be appreciated.
<script>
$(function () {
var result;
function country() {
$.getScript("ajax/country.js", function () {
result = 'data one'; // I need to get this result in location().
});
}
function location() {
console.log(result); // This result should be 'data one'. Currently, I am getting undefined.
}
country();
location();
});
</script>
$.getScript() works asynchronous. Call location in the getScript function.
$(function () {
function country() {
$.getScript("ajax/country.js", function () {
let result = 'data one'; // I need to get this result in location().
location(result);
});
}
function location(result) {
console.log(result); // This result should be 'data one'. Currently, I am getting undefined.
}
country();
});
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
I'm a bit tired and I can't see why I'm getting undefined errors on the variable cat.data[0].id.
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
function Trial() {
var index = 0;
var cat = [];
function getcat() {
.
.Makes a call to get JSON set of data in the form
.{"status" : "success", "data" : [{"id":"8", "url":"http:\/\/google.com", "description":"Search Engine"}]}
.
request.success(function (data) {
cat = $.parseJSON(data);
if (cat.status === "error") {
alert(cat.message);
}
console.log(cat.data[index].id);
alert(cat.data[0].id);
});
request.error(function (data, status) {
alert("Error connecting to API:" + status);
});
}
}
function tests() {
getcat();
console.log(cat.data[index].id);
}
So when we call tests function it pulls the data and puts into array and the entries for console.log and alert do exactly as they should and display the id field value. However when it returns to tests and does the console.log entry there it fails with:
"Unable to get property '0' of undefined or null reference"
It's a global variable in the master function so why isn't it able to access it?
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.
How to assign value to global variable
function ok(){
var idglobal;
$.get("<?php echo base_url('testchat/rtc/showchat'); ?>", function (data) {
data = $.parseJSON(data);
$.each(data, function (i, item) {
idglobal = item.id;
});
});
}
console.log(idglobal);
above this code to assign value to global variable but the result is undefined
You console.log isn't printing it because it runs before the promise resolves.
Try:
function ok(){
var idglobal;
$.get("<?php echo base_url('testchat/rtc/showchat'); ?>", function (data) {
data = $.parseJSON(data);
$.each(data, function (i, item) {
idglobal = item.id;
});
console.log(idglobal);
});
}
The value of idglobal should be the last item.id.
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)
});