0

I have an API endpoint for registering a user written in Python Flask. I can see on the linux console that Flask is returning a 400 when the password does not have a lower case letter in it and it returns the json data {'error':{'code':{'password_missing_lower_case_letters'}}, which I need to process and return feedback to the use, but I can't access this on the browser and all jq_xhr outputs is [object Object]. I know everything else is working because when I do add a lower case letter, the user is successfully registered. What am I doing wrong that prevents me from reading the returned data on error from the jqxhr object?

$.ajax({
    type: 'POST',
    url: `${g_domain__api}/register_user`,
    data: JSON.stringify(data),
    datatype: 'json',
    contentType: 'application/json; charset=utf-8',
    success: function (data, text_status, jq_xhr) {
        console.log ('I am happy!');
    },
    error: function (jq_xhr, text_status, error_thrown) {
        //I'm getting desperate here and logging everything I can
        console.log(`jq_xhr-> ${jq_xhr}`);
        console.log(`text_status-> ${text_status}`);
        console.log(`error_thrown-> ${error_thrown}`);
    }
});

Browser console output:

jq_xhr-> [object Object]
text_status-> error
error_thrown-> BAD REQUEST
puk
  • 16,318
  • 29
  • 119
  • 199
  • It only outputs `[object Object]` because you are treating the object as a string in your logging statement and that is the `toString()` output of a js object. Log the object itself without putting it in a template literal – charlietfl Feb 15 '21 at 21:46
  • @charlietfl when am I calling `toString()`? and how do I "Log the object itself without putting it in a template literal" – puk Feb 15 '21 at 22:45
  • 1
    You are using [Template Literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) syntax to log an object. In order to compile that template literal the js engine has to use the `toString()` representation. Change to using separate arguments `console.log('jq_xhr->', jq_xhr);` and log the actual object – charlietfl Feb 15 '21 at 22:56
  • @charlietfl it worked. if you put it as an answer I will accept it. THanks – puk Feb 15 '21 at 23:08

0 Answers0