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.
Related
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 5 years ago.
I have the following code:
function test() {
$.when(MyModule.loadData("breadcrumbs", "BreadcrumbsData", jsndata, 'GET')).then(
function (data) {
return data;
});
}
var result = test();
Problem: I am not getting the data in 'result' variable.
I am using Jquery 3.x.
MyModule.loadData is a method that executes a synchronous ajax request and returns the jqXHR object.
Thanks.
function test() {
return $.when(MyModule.loadData("breadcrumbs", "BreadcrumbsData", jsndata, 'GET')).then(
function (data) {
return data;
});
}
var result;
test().then(function(data){
result = data;
}).then(function(){
console.log(result);
});
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)
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();
});