How do I get JSON data from Rest APi [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I am creating an object in javascript:
var t = null;
$.getJSON('http://localhost:53227/Home/GetData', function (data) {
alert(data);
t = data;
});
alert(t);
When I alert data, I get an object back.
When I alert t, it's null.
Can you please guide, how to set "t" to the returned data?

This will work as expected - the issue is not that t is not set, it's that you're doing alert(t) before the getJSON callback is executed. Try doing alert(t) immediately after t = data;
In other words, here's your current order of operations:
Set t = null
Call server script
alert(t) --> t is still null!
(some amount of time later) receive JSON response
alert data
set t = data
...as you can see, at step 3 't' will still be null. Try this instead:
var t = null;
$.getJSON('http://localhost:53227/Home/GetData', function (data) {
alert(data);
t = data;
alert(t);
});
Cheers

Related

Ajax() response not received on time? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I have tried too many codes to achieve the following:
$(document).ready(function(){
var json_data = $.getJSON('url_to_json_encoded_array.php',function(j){
return j;
});
});
if(json_data.length > 0){
//fetch data from the json_data json ecoded array here
}
But after running the code I always get json_data is with no data, while in developer tab -> network-> i can see that the data loaded in proper mode....
php code which returning the json encoded array is as following:
<?php
echo(json_encode(array("name"=>$name,"email"=>$email)));
?>
What is wrong in my code?
Your request is async, you will receive your data inside the callback function
$.getJSON('url_to_json_encoded_array.php', function(data) {
// here you have it
console.log(data);
});

Javascript not returning request [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
function getCatFact() {
var catFact = "";
request('http://catfacts-api.appspot.com/api/facts', function (error, resp, body) {
var jsonObj = JSON.parse(body);
catFact += jsonObj.facts[0];
})
console.log("HERE IT IS: "+catFact);
return "Here is your CatFact" + catFact;
}
Seems very simple but I am doing something wrong. All this function returns is Here is your CatFact.. I tried putting the return inside the request function as well, with no luck. Would love any help.
The request is asynchronous. That means that the request will fire, and JavaScript will immediately move on the the next line before the response has made it back to the client. In your case, the stuff that prints the cat fact.
Returning the cat fact itself will not work in this instance, because you will return it before the response has returned and changed the empty string. You either need to call a function within the success callback or return the request object itself.

How to use javascript variable out of the scope? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
I am using $.get functionality to get json data from action method. But out of $.get() function JavaScript variable getting default value.
code look like:
var data = 0;
$(document).ready(function () {
var url = "/Controller/Action";
$.get(url, null, function (Data) {
data = JSON.stringify(Data);
console.log(data);
});
console.log(data);
});
Output display look like:
[{"Name":"Jatin","Address":"surat","colorScheme":"#1696d3"},{"Name":"Jatin","Address":"surat","colorScheme":"#1696d3"},{"Name":"Jatin","Address":"surat","colorScheme":"#1696d3"}]
And then
Display 0.
How may I use Data value out of scope?
Don't declare 'var' in front of data in the $.get call. This declares a new variable. Since data is already defined outside the function scope, simple change the value by doing "data = JSON.stringify(Data);"
var data = 0;
$(document).ready(function () {
var url = "/Controller/Action";
$.get(url, null, function (Data) {
data = JSON.stringify(Data);
console.log(data);
});
console.log(data);
});
Edit: After chatting to OP, he was trying to use the response data from $.get in a synchronous manner. For that, I suggested either moving the code that relies on the response data in an ajax success function or use jquerys $.when() function

Unable to store value in global variable JS [duplicate]

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.
I have a following piece of javaScript code:
var ws_path = "x";
$.get('ws.config', function(data) {
ws_path = data;
alert(ws_path); //alert 1
},
'text');
alert(ws_path); // alert 2
// output = alert 1 = value of data
// alert 2 = x
I have a confusion' why it is behaving like this?
It is because alert(ws_path); gets executed before the content is get from server for ws.config file. You need to write the code in success function of get to ensure that variable is modified after get request.
jQuery.get
The second alert is fired before the $.get request is completed.
The important point here is:
$.get('ws.config' ....
is a kind of ajax call and it takes some time to get the actual value from the server, and that anonymous function there is a callback function which gets called when the ajax call receives the response.
Here in your code if you want to have a ordered scenario you can do this:
var ws_path = "x";
$.get('ws.config', function(data) {
ws_path = data;
alert(ws_path);
continueScenario();
}, 'text');
function continueScenario(){
alert(ws_path);
}

How to get the data from Jquery.get to a variable in Javascript [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I want to load a data that I got from Jquery.get into the variable in another function. The way I have tried is
function setPolicy(){
jQuery.get('/webapp/myProject/getPolicy', function(policy) {
console.log(policy + " Policy"); //Just for Observation
}); }
and I want to load the data which is now stored in the Parameter Name "policy" into another variable in another function like the following
function loadAPI() {
var policy = setPolicy();
console.log("The policy is " + policy);
The result Shows in the console is just
The policy is
which mean the variable policy is not receive the data from the function.
What I would like to ask is How can I load the data from function "setPolicy" into the variable Name "policy" in another function calls "loadAPI" as a string? Thank you in advance.
You need to return the fetched value. Try to add this stmt in your code -
$.get('URL', function(data) {
$('.result').html(data);
return data;
});

Categories

Resources