-2

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?

user2896120
  • 3,180
  • 4
  • 40
  • 100

1 Answers1

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)

apple apple
  • 10,292
  • 2
  • 16
  • 36
  • I see, what is the best way of fixing this? – user2896120 Jan 01 '19 at 08:11
  • the basic would be `return $.ajax(...)`, but you should learn about `Promise` to make use of it. – apple apple Jan 01 '19 at 08: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 01 '19 at 08:20
  • @user2896120 I don't use jQuery, but isn't the `error` callback just for this? – apple apple Jan 01 '19 at 08: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 01 '19 at 08:24
  • @user2896120 I'd guess it's because you're trying to get `responseJSON` when it's `400`. The `responseJSON` should not even exist. – apple apple Jan 01 '19 at 08:25
  • But if the form was submitted incorrectly, i.e password did not meet requirements, wouldn't the 400 bad request entail a responseJSON indicating what the password error is? – user2896120 Jan 01 '19 at 08:28
  • @user2896120 I'm not sure what your API looks like, can you provide [mcve]? – apple apple Jan 01 '19 at 08:29
  • Hmm, I'm using Django's built in REST API. There's a registration endpoint that I submit a POST request to with all the registration details (username, email, password). If there's an error in any of these details submitted (empty fields, incorrect format, etc.) then a 400 bad request will be thrown with a JSON response indicating why each field failed. If there are no errors, a successful 200 response will be received. – user2896120 Jan 01 '19 at 08:35
  • @user2896120 then it should be fine O.o. Can you click on the console to check is it really a `exception` or just a `console.error()` by `jQuery`? – apple apple Jan 01 '19 at 08:40
  • `jquery.min.js:2 POST http://127.0.0.1:8000/rest-auth/registration/ 400 (Bad Request)` so i'm guessing it's just an error by jquery – user2896120 Jan 01 '19 at 08:45