如何安慰。这个ajax调用的日志结果?

时间:2021-09-21 16:56:43

I have the following function in jQuery code:

我在jQuery代码中有以下功能:

       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.

我想做的是:我想安慰自己。记录错误消息。如果控制台没有定义。日志行位于ajax函数之上,如果是控制台,则没有日志。圆木圆木。

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

有人能告诉我如何获取这个或另一个新函数中显示的errorMessage的值吗?

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

另外,任何关于使用Ajax检查php登录表单的链接都会被深深感激。

Regards,Zoran

问候,Zoran

7 个解决方案

#1


11  

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); }
});

#2


2  

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

您可以尝试以下操作(从jQuery Ajax示例中复制)

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.

您的原始代码的问题是,您传递到您的on函数中的错误参数实际上并不是来自任何地方。JQuery并没有返回第二个参数,即使是这样,它也与click事件相关,而不是Ajax调用。

#3


1  

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

如果调用本身失败,将触发Ajax调用错误处理程序。

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.

您可能试图从服务器获取错误,以防登录凭证没有通过。在这种情况下,您需要检查服务器响应json对象并显示适当的消息。

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:

这里的“结果”是从服务器返回的json对象,该对象可以具有如下结构:

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

#4


1  

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

#5


0  

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

如果你想检查你的URL。我想你用的是Chrome浏览器。你可以到chrome控制台,URL将显示在“XHR完成加载:”

#6


0  

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

                  }
});

#7


-1  

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

在Chrome中,右键点击控制台,检查“保存导航”。

#1


11  

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); }
});

#2


2  

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

您可以尝试以下操作(从jQuery Ajax示例中复制)

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.

您的原始代码的问题是,您传递到您的on函数中的错误参数实际上并不是来自任何地方。JQuery并没有返回第二个参数,即使是这样,它也与click事件相关,而不是Ajax调用。

#3


1  

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

如果调用本身失败,将触发Ajax调用错误处理程序。

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.

您可能试图从服务器获取错误,以防登录凭证没有通过。在这种情况下,您需要检查服务器响应json对象并显示适当的消息。

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:

这里的“结果”是从服务器返回的json对象,该对象可以具有如下结构:

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

#4


1  

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

#5


0  

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

如果你想检查你的URL。我想你用的是Chrome浏览器。你可以到chrome控制台,URL将显示在“XHR完成加载:”

#6


0  

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

                  }
});

#7


-1  

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

在Chrome中,右键点击控制台,检查“保存导航”。