Jquery: Return Data After Ajax Call Success

jQuery: Return data after ajax call success

The only way to return the data from the function would be to make a synchronous call instead of an asynchronous call, but that would freeze up the browser while it's waiting for the response.

You can pass in a callback function that handles the result:

function testAjax(handleData) {
$.ajax({
url:"getvalue.php",
success:function(data) {
handleData(data);
}
});
}

Call it like this:

testAjax(function(output){
// here you use the output
});
// Note: the call won't wait for the result,
// so it will continue with the code here while waiting.

How to return data to variable after ajax call success

$.ajax({
url:"operation.php",
dataType: "text",
success:function(data) {
doSomthingOnComplete(data);
}
});

function doSomthingOnComplete(data)
{
// do here your work
}

How to return data from ajax success function?

Instead of returning data from success: pass data to a function.

var view_data;
$.ajax({
url:"/getDataFromServer.json",
//async: false,
type: "POST",
dataType: "json",
success:function(response_data_json) {
view_data = response_data_json.view_data;
console.log(view_data); //Shows the correct piece of information
doWork(view_data); // Pass data to a function
}
});

function doWork(data)
{
//perform work here
}

jquery - return value using ajax result on success

The trouble is that you can not return a value from an asynchronous call, like an AJAX request, and expect it to work.

The reason is that the code waiting for the response has already executed by the time the response is received.

The solution to this problem is to run the necessary code inside the success: callback. That way it is accessing the data only when it is available.

function isSession(selector) {
$.ajax({
type: "POST",
url: '/order.html',
data: ({ issession : 1, selector: selector }),
dataType: "html",
success: function(data) {
// Run the code here that needs
// to access the data returned
return data;
},
error: function() {
alert('Error occured');
}
});
}

Another possibility (which is effectively the same thing) is to call a function inside your success: callback that passes the data when it is available.

function isSession(selector) {
$.ajax({
type: "POST",
url: '/order.html',
data: ({ issession : 1, selector: selector }),
dataType: "html",
success: function(data) {
// Call this function on success
someFunction( data );
return data;
},
error: function() {
alert('Error occured');
}
});
}

function someFunction( data ) {
// Do something with your data
}

How to use response data from ajax success function out of Ajax call

The problem is that by default Ajax request is async

which means that ajax will start the request
then execute: alert(getData[name]); then finish the request in the background and call success function.

so actually the alert will execute before success function.
and to do what you want you have to tell ajax not to execute any thing before it done, in other ward set async: false
Second thing you have to declare the variable outside the ajax scope so you can access it outside ajax

The final code will be :

var getData;
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
async: false,
success: function (response) {
getData[name] = response;

}
});
alert(getData[name]);

jQuery AJAX: return value on success

You'd better change your approach to reflect an asynchronous nature of AJAX request.

Using callback function

function ChatServerQuery(data, callback) {
$.ajax({
url: 'chat/backend/',
type: 'POST',
data: data,
success: callback
});
}

Then you would use it:

ChatServerQuery(dataObject, function(data) {
// work with your data came from server
});

Using promise object

$.fn.ajax returns object implementing Promise iterface, so you can use it like this:

function ChatServerQuery(data) {
return $.ajax({
url: 'chat/backend/',
type: 'POST',
data: data
});
}

ChatServerQuery(dataObject).done(function(data) {
// work with your data came from server
});

This option offers you more flexibility.

jQuery return ajax result into outside variable

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.

function that return a value from ajax call request

You need to register a callback function, something like this:

function test() {
myFunction(function(d) {
//processing the data
console.log(d);
});
}

function myFunction(callback) {
var data;
$.ajax({
url: 'url',
data: 'data to send',
success: function (resp) {
data = resp;
callback(data);
},
error: function () {}
}); // ajax asynchronus request
//the following line wouldn't work, since the function returns immediately
//return data; // return data from the ajax request
}


Related Topics



Leave a reply



Submit