Returning the response from an Ajax call
Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
-
Marius
- Posts: 106
Returning the response from an Ajax call
Hi,
I have a function sendAjax() which makes an Ajax request (with jQuery). How can I return the response from sendAjax()?
I tried to return the value from the success response, as well as assigning the response to a local variable inside the function and return that one, but none of those ways actually return the response.
Code: Select all
function sendAjax(url, datasend) {
var result;
$.ajax({
url: url,
data: datasend,
success: function(response) {
result = response;
// return response; // <- tried that one as well
}
});
return result;
}
var result = sendAjax('file.php', 'id=9'); // always ends up being `undefined`
MarPlo
Hi,
I usually use a callback function that is passed as argument to the Ajax function.
The callback function is accessed when success, receives the response, and handles it.
Code: Select all
function sendAjax(url, datasend, callback) {
$.ajax({
url: url,
data: datasend,
success: function(response) {
callback(response);
}
});
}
// the callback function
function ajaxRe(resp) {
// do whatever you want with resp
}
sendAjax('file.php', 'id=9', ajaxRe);
Similar Topics
-
Foreach JSON RESPONSE from Api
PHP - MySQL
First post
Hello, i have a JSON response from an api that is like this: mluci.com/script.php
I tried to transform this to an array but i can't foreach this.For...
Last post
I resolved with this thank you:
I changed the code but this send only an api request :
<?php
$servername = localhost ;
$username = mluci_api ;...
-
Problem with 'this' in function.call() in JavaScript
JavaScript - jQuery - Ajax
First post
Why the function.call() behaves differently with and without ' this ', in JavaScript?
The result with 'this' in test.call() is same when 'this' is...
Last post
The call() function require the first parameter as 'this' object, if you do not need it, just pass null .
test.call(null, ...args);
In your...
-
Ajax-PHP File Manager with CKEditor
Scripts from this website
First post
Loving your Ajax-PHP File Manager, great job! - coursesweb.net/php-mysql/ajax-php-file-manager_s2
Was thinking to using this in classroom project...
Last post
Yes, I understand it does not need ckeditor to run. I was asking for help in incorporating ckeditor into it for the kids.
Sorry to have bothered...
-
Execution order with jQuery-AJAX
JavaScript - jQuery - Ajax
First post
I have this code (ajax is async):
function echoHello(){
return $.ajax({
//this will return hello ;
});
}
function echoWorld(){
return world...
Last post
Ajax makes a call to the web server and is asynchronous. You don't know how long it will take. It is the same as:
setTimeout(_ => {...
-
Confirmed error in Ajax Voting Script
Announcements & Support
First post
Although it took me about eight hours to get it working, the Ajax Voting Scipt is very useful, and the best freeware script of its kind that I've...
Last post
Thank you for your observation.
I corrected the error.