Ajax return call for error is not returning [duplicate]
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
I have an Ajax call inside a function like so:
function send_data(url, data) {
$.ajax({
url: url,
type: 'POST',
data: data,
success: function (data) {
//Empty for now
},
error: function (xhr) {
return xhr.responseJSON;
},
});
}
and then I trigger this function like so:
console.log(send_data(url, data));
I should receive the response output, but I don't. I instead, get undefined. I know JavaScript is asynchronous, but shouldn't this work? I added a console.log
output inside the error function, before the return statement, and it outputs the data correctly. So why doesn't it return the data?
javascript jquery ajax
marked as duplicate by adiga, CertainPerformance
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 8:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
I have an Ajax call inside a function like so:
function send_data(url, data) {
$.ajax({
url: url,
type: 'POST',
data: data,
success: function (data) {
//Empty for now
},
error: function (xhr) {
return xhr.responseJSON;
},
});
}
and then I trigger this function like so:
console.log(send_data(url, data));
I should receive the response output, but I don't. I instead, get undefined. I know JavaScript is asynchronous, but shouldn't this work? I added a console.log
output inside the error function, before the return statement, and it outputs the data correctly. So why doesn't it return the data?
javascript jquery ajax
marked as duplicate by adiga, CertainPerformance
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 8:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
I have an Ajax call inside a function like so:
function send_data(url, data) {
$.ajax({
url: url,
type: 'POST',
data: data,
success: function (data) {
//Empty for now
},
error: function (xhr) {
return xhr.responseJSON;
},
});
}
and then I trigger this function like so:
console.log(send_data(url, data));
I should receive the response output, but I don't. I instead, get undefined. I know JavaScript is asynchronous, but shouldn't this work? I added a console.log
output inside the error function, before the return statement, and it outputs the data correctly. So why doesn't it return the data?
javascript jquery ajax
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
I have an Ajax call inside a function like so:
function send_data(url, data) {
$.ajax({
url: url,
type: 'POST',
data: data,
success: function (data) {
//Empty for now
},
error: function (xhr) {
return xhr.responseJSON;
},
});
}
and then I trigger this function like so:
console.log(send_data(url, data));
I should receive the response output, but I don't. I instead, get undefined. I know JavaScript is asynchronous, but shouldn't this work? I added a console.log
output inside the error function, before the return statement, and it outputs the data correctly. So why doesn't it return the data?
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
javascript jquery ajax
javascript jquery ajax
asked Jan 1 at 7:57
user2896120user2896120
9861025
9861025
marked as duplicate by adiga, CertainPerformance
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 8:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by adiga, CertainPerformance
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 8:13
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
what you do is basically declare function inside function
function foo(){
function bar(){console.log('in bar')}
}
console.log(foo())
as you can see, the bar
is not called (which in your case would be called by jQuery
)
I see, what is the best way of fixing this?
– user2896120
Jan 1 at 8:11
the basic would bereturn $.ajax(...)
, but you should learn aboutPromise
to make use of it.
– apple apple
Jan 1 at 8:13
Hmm, I see. Is there a way to handle 400 bad request errors? I'm getting a bad request because let's say not all the form elements are used. so a bad request will be thrown. Is there a way to handle them?
– user2896120
Jan 1 at 8:20
@user2896120 I don't use jQuery, but isn't theerror
callback just for this?
– apple apple
Jan 1 at 8:22
So is it normal for the console to output 400 bad request each time? I was wondering if I could hide that
– user2896120
Jan 1 at 8:24
|
show 6 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
what you do is basically declare function inside function
function foo(){
function bar(){console.log('in bar')}
}
console.log(foo())
as you can see, the bar
is not called (which in your case would be called by jQuery
)
I see, what is the best way of fixing this?
– user2896120
Jan 1 at 8:11
the basic would bereturn $.ajax(...)
, but you should learn aboutPromise
to make use of it.
– apple apple
Jan 1 at 8:13
Hmm, I see. Is there a way to handle 400 bad request errors? I'm getting a bad request because let's say not all the form elements are used. so a bad request will be thrown. Is there a way to handle them?
– user2896120
Jan 1 at 8:20
@user2896120 I don't use jQuery, but isn't theerror
callback just for this?
– apple apple
Jan 1 at 8:22
So is it normal for the console to output 400 bad request each time? I was wondering if I could hide that
– user2896120
Jan 1 at 8:24
|
show 6 more comments
what you do is basically declare function inside function
function foo(){
function bar(){console.log('in bar')}
}
console.log(foo())
as you can see, the bar
is not called (which in your case would be called by jQuery
)
I see, what is the best way of fixing this?
– user2896120
Jan 1 at 8:11
the basic would bereturn $.ajax(...)
, but you should learn aboutPromise
to make use of it.
– apple apple
Jan 1 at 8:13
Hmm, I see. Is there a way to handle 400 bad request errors? I'm getting a bad request because let's say not all the form elements are used. so a bad request will be thrown. Is there a way to handle them?
– user2896120
Jan 1 at 8:20
@user2896120 I don't use jQuery, but isn't theerror
callback just for this?
– apple apple
Jan 1 at 8:22
So is it normal for the console to output 400 bad request each time? I was wondering if I could hide that
– user2896120
Jan 1 at 8:24
|
show 6 more comments
what you do is basically declare function inside function
function foo(){
function bar(){console.log('in bar')}
}
console.log(foo())
as you can see, the bar
is not called (which in your case would be called by jQuery
)
what you do is basically declare function inside function
function foo(){
function bar(){console.log('in bar')}
}
console.log(foo())
as you can see, the bar
is not called (which in your case would be called by jQuery
)
function foo(){
function bar(){console.log('in bar')}
}
console.log(foo())
function foo(){
function bar(){console.log('in bar')}
}
console.log(foo())
answered Jan 1 at 8:01
apple appleapple apple
2,6721823
2,6721823
I see, what is the best way of fixing this?
– user2896120
Jan 1 at 8:11
the basic would bereturn $.ajax(...)
, but you should learn aboutPromise
to make use of it.
– apple apple
Jan 1 at 8:13
Hmm, I see. Is there a way to handle 400 bad request errors? I'm getting a bad request because let's say not all the form elements are used. so a bad request will be thrown. Is there a way to handle them?
– user2896120
Jan 1 at 8:20
@user2896120 I don't use jQuery, but isn't theerror
callback just for this?
– apple apple
Jan 1 at 8:22
So is it normal for the console to output 400 bad request each time? I was wondering if I could hide that
– user2896120
Jan 1 at 8:24
|
show 6 more comments
I see, what is the best way of fixing this?
– user2896120
Jan 1 at 8:11
the basic would bereturn $.ajax(...)
, but you should learn aboutPromise
to make use of it.
– apple apple
Jan 1 at 8:13
Hmm, I see. Is there a way to handle 400 bad request errors? I'm getting a bad request because let's say not all the form elements are used. so a bad request will be thrown. Is there a way to handle them?
– user2896120
Jan 1 at 8:20
@user2896120 I don't use jQuery, but isn't theerror
callback just for this?
– apple apple
Jan 1 at 8:22
So is it normal for the console to output 400 bad request each time? I was wondering if I could hide that
– user2896120
Jan 1 at 8:24
I see, what is the best way of fixing this?
– user2896120
Jan 1 at 8:11
I see, what is the best way of fixing this?
– user2896120
Jan 1 at 8:11
the basic would be
return $.ajax(...)
, but you should learn about Promise
to make use of it.– apple apple
Jan 1 at 8:13
the basic would be
return $.ajax(...)
, but you should learn about Promise
to make use of it.– apple apple
Jan 1 at 8:13
Hmm, I see. Is there a way to handle 400 bad request errors? I'm getting a bad request because let's say not all the form elements are used. so a bad request will be thrown. Is there a way to handle them?
– user2896120
Jan 1 at 8:20
Hmm, I see. Is there a way to handle 400 bad request errors? I'm getting a bad request because let's say not all the form elements are used. so a bad request will be thrown. Is there a way to handle them?
– user2896120
Jan 1 at 8:20
@user2896120 I don't use jQuery, but isn't the
error
callback just for this?– apple apple
Jan 1 at 8:22
@user2896120 I don't use jQuery, but isn't the
error
callback just for this?– apple apple
Jan 1 at 8:22
So is it normal for the console to output 400 bad request each time? I was wondering if I could hide that
– user2896120
Jan 1 at 8:24
So is it normal for the console to output 400 bad request each time? I was wondering if I could hide that
– user2896120
Jan 1 at 8:24
|
show 6 more comments