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);
});
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
Code:
<script>
$(document).ready(function(){
var filepath = 'csv/data.csv';
var data_string = $.get(filepath);
console.log(data_string);
});
</script>
When I use console.log(data_string) I got the following output on the console.
When I read that, I found that "responseText" has the values which I want. So I just need to get "responseText" to another variable.
I tried var data = data_string.responseText. But it not worked.
You should attach a callback function to $.get. From your console i see that is a jqXHR Object. $.get method has a success callback function that is executed if the request succeeds.
Attaching a callback function includes automatically json parsed.
var filepath = 'csv/data.csv';
$.get( filepath , function(response) {
console.log(response);
});
You need to provide callback function to get response from server like below:
var filepath = 'csv/data.csv';
$.get( filepath , function( data ) {
console.log(data);
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I'm importing some data with PHP, which gets data from an SQL database. I'm using AJAX to import the data from the PHP to Javascript. The PHP code works fine and results in "2", but something's wrong with my Javascript code:
<script>
$.getJSON("Kategorie1.php", function (data) {
window.nrFragen = JSON.parse(data);
window.alert(data)
});
window.alert(window.nrFragen);
</script>
If I run it, it first runs window.alert(window.nrFragen) which alerts undefined and then window.alert(data), which alerts "2", as it should.
Why does it first run the window.alert(window.nrFragen), even though it's written after window.alert(data)? And, why isn't window.nrFragen = JSON.parse(data); working?
$.getJSON is async.
$.getJSON("Kategorie1.php", function (data) {
//this will be called only after request completion
window.nrFragen = JSON.parse(data);
window.alert(data)
});
//this will be called immediately after $.getJSON, it won't wait for request completion
window.alert(window.nrFragen);
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);
}
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
This the data im getting from post is a simple object and the return value is always empty. Its logging to the console fine. any ideas?
function getTaskData(item){
var returnText = '';
$.post("index.php", {name: "getTaskData", pk: item.taskDataId}, function(data){
console.log(data);///Object {taskData: "Also - whats up with this?"}
console.log(data.taskData);///Also - whats up with this?
returnText = data.taskData;
},"json");
return returnText;
}
The code included in the block function(data) { ... is called a callback. It will be aynchronously called when the server's ajax call has finished. Whereas the return returnText will be called immediately.
Whatever you are going to do with the returntext (update DOM etc) will need to be done in the callback function
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