jquery Cannot read property 'done' of undefined - avoid this

前端 未结 1 912
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 18:51

I have a function which returns results (or not). The problem is when it does not return any value I get in the console the message

cannot read prope

相关标签:
1条回答
  • 2021-01-02 19:17

    You could just return a deferred, that way the done() callback won't generate errors, and you can choose to resolve it or not

    if(items.length > 0){
        return $.ajax({
            url: 'response.php?type=getDelivery',
            type: 'POST',
            data: {content: items}
        });
    }else{
        var def = new $.Deferred();
        def.resolve(false);
        return def;
    }
    

    FIDDLE

    0 讨论(0)
提交回复
热议问题