jQuery 这个ajax调用的console.log结果如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12025813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:12:15  来源:igfitidea点击:

how to console.log result of this ajax call?

jqueryajax

提问by Zoran

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.

我正在尝试做什么:我正在尝试使用 console.log 记录错误消息。如果 console.log 行在 ajax 函数之上,我会变得未定义,如果 console.log 在它下面,则什么都没有。

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

问候,卓然

回答by Qiau

Why not handle the error within the call?

为什么不处理调用中的错误?

i.e.

IE

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

回答by Nick

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 on 不返回第二个参数,即使返回,它也将与单击事件相关,而不是与 Ajax 调用相关。

回答by Gamul Mijo

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

回答by nsm

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

回答by Rohit

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:

这里,“result”是从服务器返回的 json 对象,它的结构可能如下:

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

回答by Jeffery

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

如果您想检查您的网址。我想你正在使用 Chrome。您可以转到 chrome 控制台,URL 将显示在“XHR 已完成加载”下:

回答by weaveoftheride

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

在 Chrome 中,右键单击控制台并选中“保留登录导航”。