javascript JSON 或 Jquery 错误:未捕获的类型错误:无法读取 null 的属性“错误”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19239290/
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-10-27 14:51:12  来源:igfitidea点击:

JSON or Jquery error: Uncaught TypeError: Cannot read property 'error' of null

javascriptjqueryjson

提问by Dj Ike

I am trying to make an ajax call using the below jQuery.

我正在尝试使用以下 jQuery 进行 ajax 调用。

But I can see in Chrome, i'm getting uncaught typeerror cannot read property 'error' of null. So this stops the preloader from going away. Any idea why it's doing this?

但是我可以在 Chrome 中看到,我得到了未捕获的类型错误无法读取 null 的属性“错误”。所以这会阻止预加载器消失。知道为什么要这样做吗?

function appendTeam(){

$.ajax({

       url : _path + "/core/ajax.php",
       type : 'POST',
       data : { f: 'getTeam'},
       dataType : 'json',
       success :  function(data) {

        if(data.error){

            errorMessage('Error: ' + data.error, data.error, data.error);
            return false;

        }else{
            var count = 0;
            $.each(data.team, function(i, c){
                // check
                if(!$('#'+c)) return true;
                var element = $('#'+c);
                $('input[name="s'+i+'"]').val(element.attr('id'));
                $('.slot.'+(i+1)+'.ui-droppable').append(element);
                element.data('prevParent', $('.slot.'+(i+1)+'.ui-droppable'));
                count ++;

            });

            appendStatus(count);
            setTimeout(function(){
                $('#preloader').fadeOut('fast',function(){
                    $('#preloader').remove();
                    popUp('match');
                });
            }, 2000);


        }

    }
});
}

回答by Eugene Naydenov

There is a mistake in your ifoperator:

您的if操作员有错误:

if(data.error)...

if(data.error)...

You should check it like if(data && data.error)...so if the server returned you nullinstead of JSON object you won't try to access object's property error.

您应该像if(data && data.error)...这样检查它,如果服务器返回您null而不是 JSON 对象,您将不会尝试访问对象的属性error

Also maybe you need to handle this case separately:

也可能您需要单独处理这种情况:

if(!data) {
    // handle empty response
} else if(data.error) {
    // handle error
} else {
    // process results
}

回答by Pascalz

Test if datais not nullbefore trying acces his errorproperty

在尝试访问他的财产之前测试是否data不是nullerror

回答by Vishal

Probably the dataobject does not contains the property erroror it is null;

可能data对象不包含该属性error或它为空;

so instead of this

所以而不是这个

if(data.error) {

if(data.error) {

you can check for it in a different way :

你可以用不同的方式检查它:

if(data && data.hasOwnProperty('error')) {

if(data && data.hasOwnProperty('error')) {

which is more fail-safe.

这更安全。

回答by Pankaj Sharma

i think response is null try to see response in using firebug enter image description here

我认为响应为空尝试查看使用萤火虫的响应 在此处输入图片说明