Ajax return call for error is not returning [duplicate]












-2
















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?










share|improve this question













marked as duplicate by adiga, CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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.























    -2
















    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?










    share|improve this question













    marked as duplicate by adiga, CertainPerformance javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    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.





















      -2












      -2








      -2









      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?










      share|improve this question















      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 7:57









      user2896120user2896120

      9861025




      9861025




      marked as duplicate by adiga, CertainPerformance javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      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 javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      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.


























          1 Answer
          1






          active

          oldest

          votes


















          2














          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)






          share|improve this answer
























          • 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













          • 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











          • 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


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          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)






          share|improve this answer
























          • 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













          • 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











          • 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
















          2














          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)






          share|improve this answer
























          • 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













          • 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











          • 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














          2












          2








          2







          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)






          share|improve this answer













          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())






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 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











          • @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



















          • 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













          • 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











          • 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





          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas