This question already has answers here:
getting a response from $.post
(2 answers)
Closed 9 years ago.
I want geting data out of jQuery podt, How can do it?
nat="";
$.post("random_unique", function(data){
nat = data;
});
alert(nat); //--> Nothing.
jQuery calls are asynchronous, in that you can execute the remainder if your script while the data is still loading. Perform your tasks in the callback of your $.post().
nat="";
$.post("random_unique", function(data){
nat = data;
alert(nat); //--> Something!
});
P.S. don't use alert() to debug code; use console.log(). If you receive a JSON response (or when you get into error handling) often times you'll be working with objects, which don't alert out.
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)
Closed 7 years ago.
My chrome extensions Content script uses a chrome.runtime.sendMessage to send a message to my Background script, which then needs to read the cookies and return these in the 'sendResponse'.. code below:
ContentScript.js
chrome.runtime.sendMessage({getcookies: "Get me cookies"}, function (response){
console.log(response.cookieList);
});
BackgroundScript.js
var cookiesFinal;
chrome.runtime.onMessage.addListener(function(res, sender, sendResponse) {
chrome.cookies.get({ url: 'https://www.example.com/', name: 'cookie1' }, function GetCookies(cookies) {
cookiesFinal = cookies;
});
sendResponse(cookiesList: cookiesFinal);
});
However when I run this I get an error saying 'cannot read value of cookieList: undefined?
Any tips on how I can make this work?
I can see 2 problems:
sendResponse(cookiesList: cookiesFinal) is not in JSON format. It should be sendResponse({cookiesList: cookiesFinal})
The callback for chrome.cookies.get is asynchronous. So, cookiesFinal might still be undefined when you're sending the response before you're getting the cookies back.
The way around it can be sending another message to the content script when you've retrieved the cookies in the callback function.
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:
jQuery: Return data after ajax call success [duplicate]
(5 answers)
Closed 8 years ago.
I am using jQuery to pull in data from a csv. The request is successful (Success!! in the console) and I can see the data in the responseText field when I print the object but I can't print data.responseText (shows in console as "undefined").
window.onload = function(){
console.log("start");
var data = $.ajax({url:"http://localhost/bootstrap/data/930.csv",success:function(){
console.log("Success!!");
}()
});
console.log(data);
console.log(data.responseText);
How do I access responseText to transform it?
EDIT:
Updated my code per comments to force sync and modified the variables to better follow them. Still I was surprised by the result.
console.log("start");
var ajaxData = $.ajax({url:"http://localhost/bootstrap/data/930.csv",async:false,success:function(dataReturned){
console.log("Success!!"); //Success!!
console.log(dataReturned); //Returns my csv data
console.log(dataReturned.responseText); //undefined
}
});
console.log(ajaxData); //Returns a jQuery object that included my csv data
console.log(ajaxData.status); // Returns 200
console.log(ajaxData.responseText); //Returns my data (same as dataReturned in success function)
So I guess I also missed that the jQuery object isn't created and available until the complete $.ajax call finishes but the response is available sooner.
Thanks for the help.
Please learn about asynchronous methods. You need to use the callback. What you tried to do is eat a delivery pizza right after you hung up the phone. You need to wait until the guy delivers it.
$.ajax({url:"http://localhost/bootstrap/data/930.csv",success:function(data, status, xhr){
console.log("Success!!");
console.log(data);
console.log(xhr.responseText);
}
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I have been working on this for the past 2 hours. I am getting the error, SyntaxError: Unexpected token u when I try to parse a GET request using a function. But I perform the same code one line at a time it works fine.
I noticed that as soon as I create an object its readyState is 1, but right after I save it and wait a second the readyState changes to 4 then it parses fine.
I thought that maybe the XmlHttpObject I am pulling just needs to communicate to the server after the object is already on my computer, like maybe it is not done pulling all information and once the information pull is complete it turns it to 4. As a result of this realization I tried to use the timeout function to wait a few seconds then try to parse it, but I still couldn't get it to work!
Here is my code:
function pullData(){
var obj = $.get("https://api.parse.com/1/classes/chats");
var object_array = JSON.parse(obj.responseText);
return object_array
}
function pullData(callbackFunction)
$.get( "https://api.parse.com/1/classes/chats", function( data ) {
var object_array = JSON.parse(data);
callbackFunction(object_array);
});
}
JavaScript ajax calls are asynchronous, so u can't get the result at next line code after you perform get request, because get request is executed in background.
JQuery has a great documentation to look how to write ajax requests. http://api.jquery.com/jquery.get/
Why don't you try
function pullData(){
$.get("YOUR_URL")
.done(function(data) {
var object_array = JSON.parse(data);
return object_array
})
}
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to access a variable set within an Ajax call
I am struggling a little, I have a ajax post, and I get a number returned from the call.
My question is, how do I get the result number into a variables that can be seen outside the $.post call ?
E.g:
//code here
$.post('../utility/width.php', { adress:adr } ,
function(width) {
//this width result, I want it to be used
});
// here
//code here
AJAX is asynchronous. You'll have to use a callback function that gets called when the AJAX request finishes:
$.post('../utility/width.php',
{adress:adr},
function(width) {
alert(width);
}
});
An alternative would be to make the request synchronous, but that probably isn't something you want.