Question

[Solved] how to console.log result of this ajax call?

I have the following function in jQuery code:

btnLogin.on('click', function(e,errorMessage){
    console.log('my message' + errorMessage);
    e.preventDefault();

    return $.ajax({
        type: 'POST',
        url: 'loginCheck',
        data: $(formLogin).serialize(),
        dataType: 'json'  
    }).promise();
    console.log('my message' + errorMessage);
});

WHAT I AM TRYING TO DO: I am trying to console.log the error message. I am getting undefined if the console.log line is above the ajax function, and nothing if the console.log is bellow of it.

Can anyone tell me how to get the value of the errorMessage displayed in this or another new function?

Also, any link with about using Ajax for checking php login form will be deeply appreciagted

Regards,Zoran

Enquirer: Zoran

||

Solution #1:

Why not handle the error within the call?

i.e.

$.ajax({
    type: 'POST',
    url: 'loginCheck',
    data: $(formLogin).serialize(),
    dataType: 'json',
    error: function(req, err){ console.log('my message' + err); }
});
Respondent: Qiau

Solution #2:

You could try something like this (copied from the jQuery Ajax examples)

var request = $.ajax({
  url: "script.php",
  type: "POST",
  data: {id : menuId},
  dataType: "html"
});

request.done(function(msg) {
  console.log( msg );
});

request.fail(function(jqXHR, textStatus) {
  console.log( "Request failed: " + textStatus );
});

The problem with your original code is that the error argument you pass into your on function isn’t actually coming from anywhere. JQuery on doesn’t return a second argument, and even if it did, it would relate to the click event not the Ajax call.

Respondent: Nick

Solution #3:

try something like this :

$.ajax({
    type: 'POST',
    url: 'loginCheck',
    data: $(formLogin).serialize(),
    dataType: 'json',
    success: function (textStatus, status) {
        console.log(textStatus);
        console.log(status);
    },
    error: function(xhr, textStatus, error) {
        console.log(xhr.responseText);
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
    }
});
Respondent: Gamul Mijo

Solution #4:

$.ajax({
    type: 'POST',
    url: 'loginCheck',
    data: $(formLogin).serialize(),    
    success: function(result){
        console.log('my message' + result);
    }
});
Respondent: nsm

Solution #5:

Ajax call error handler will be triggered if the call itself fails.

You are probably trying to get the error from server in case login credentials do not go through. In that case, you need to inspect the server response json object and display appropriate message.

e.preventDefault();
$.ajax(
{
    type: 'POST',
    url: requestURI,
    data: $(formLogin).serialize(),
    dataType: 'json',
    success: function(result){
        if(result.hasError == true)
        {
            if(result.error_code == 'AUTH_FAILURE')
            {
                //wrong password
                console.log('Recieved authentication error');
                $('#login_errors_auth').fadeIn();
            }
            else
            {
                //generic error here
                $('#login_errors_unknown').fadeIn();
            }
        }
    }
});

Here, “result” is the json object returned form the server which could have a structure like:

$return = array(
        'hasError' => !$validPassword,
        'error_code' => 'AUTH_FAILURE'
);
die(jsonEncode($return));
Respondent: Rohit

Solution #6:

If you want to check your URL. I suppose you are using Chrome. You can go to chrome console and URL will be displayed under “XHR finished loading:”

Respondent: Jeffery

Solution #7:

In Chrome, right click in the console and check ‘preserve log on navigation’.

Respondent: weaveoftheride

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy