I have some problem using ajax.
How can I assign all result from ajax into outside variable ?
I google it up and found this code..
var return
So this is long after the initial question, and technically it isn't a direct answer to how to use Ajax call to populate exterior variable as the question asks. However in research and responses it's been found to be extremely difficult to do this without disabling asynchronous functions within the call, or by descending into what seems like the potential for callback hell. My solution for this has been to use Axios. Using this has dramatically simplified my usages of asynchronous calls getting in the way of getting at data.
For example if I were trying to access session variables in PHP, like the User ID, via a call from JS this might be a problem. Doing something like this..
async function getSession() {
'use strict';
const getSession = await axios("http:" + url + "auth/" + "getSession");
log(getSession.data);//test
return getSession.data;
}
Which calls a PHP function that looks like this.
public function getSession() {
$session = new SessionController();
$session->Session();
$sessionObj = new \stdClass();
$sessionObj->user_id = $_SESSION["user_id"];
echo json_encode($sessionObj);
}
To invoke this using Axios do something like this.
getSession().then(function (res) {
log(res);//test
anyVariable = res;
anyFunction(res);//set any variable or populate another function waiting for the data
});
The result would be, in this case a Json object from PHP.
{"user_id":"1111111-1111-1111-1111-111111111111"}
Which you can either use in a function directly in the response section of the Axios call or set a variable or invoke another function.
Proper syntax for the Axios call would actually look like this.
getSession().then(function (res) {
log(res);//test
anyVariable = res;
anyFunction(res);//set any variable or populate another function waiting for the data
}).catch(function (error) {
console.log(error);
});
For proper error handling.
I hope this helps anyone having these issues. And yes I am aware this technically is not a direct answer to the question but given the answers supplied already I felt the need to provide this alternative solution which dramatically simplified my code on the client and server sides.
'async': false
says it's depreciated. I did notice if I run console.log('test1');
on ajax success, then console.log('test2');
in normal js after the ajax function, test2
prints before test1
so the issue is an ajax call has a small delay, but doesn't stop the rest of the function to get results. The variable simply, was not set "yet", so you need to delay the next function.
function runPHP(){
var input = document.getElementById("input1");
var result = 'failed to run php';
$.ajax({ url: '/test.php',
type: 'POST',
data: {action: 'test'},
success: function(data) {
result = data;
}
});
setTimeout(function(){
console.log(result);
}, 1000);
}
on test.php (incase you need to test this function)
function test(){
print 'ran php';
}
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = htmlentities($_POST['action']);
switch($action) {
case 'test' : test();break;
}
}
You are missing a comma after
'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' }
Also, if you want return_first
to hold the result of your anonymous function, you need to make a function call:
var return_first = function () {
var tmp = null;
$.ajax({
'async': false,
'type': "POST",
'global': false,
'dataType': 'html',
'url': "ajax.php?first",
'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' },
'success': function (data) {
tmp = data;
}
});
return tmp;
}();
Note ()
at the end.
This is all you need to do:
var myVariable;
$.ajax({
'async': false,
'type': "POST",
'global': false,
'dataType': 'html',
'url': "ajax.php?first",
'data': { 'request': "", 'target': 'arrange_url', 'method': 'method_target' },
'success': function (data) {
myVariable = data;
}
});
NOTE: Use of "async" has been depreciated. See https://xhr.spec.whatwg.org/.
Using 'async': false to prevent asynchronous code is a bad practice,
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. https://xhr.spec.whatwg.org/
On the surface setting async to false fixes a lot of issues because, as the other answers show, you get your data into a variable. However, while waiting for the post data to return (which in some cases could take a few seconds because of database calls, slow connections, etc.) the rest of your Javascript functionality (like triggered events, Javascript handled buttons, JQuery transitions (like accordion, or autocomplete (JQuery UI)) will not be able to occur while the response is pending (which is really bad if the response never comes back as your site is now essentially frozen).
Try this instead,
var return_first;
function callback(response) {
return_first = response;
//use return_first variable here
}
$.ajax({
'type': "POST",
'global': false,
'dataType': 'html',
'url': "ajax.php?first",
'data': { 'request': "", 'target': arrange_url, 'method': method_target },
'success': function(data){
callback(data);
},
});
I solved it by doing like that:
var return_first = (function () {
var tmp = $.ajax({
'type': "POST",
'dataType': 'html',
'url': "ajax.php?first",
'data': { 'request': "", 'target': arrange_url, 'method':
method_target },
'success': function (data) {
tmp = data;
}
}).done(function(data){
return data;
});
return tmp;
});