0

jQuery Ajax 関数を使用して、WordPress テーマにいくつかのデモ データをインストールします。以下のこのスクリプトは、私が取り組んだ以前のテーマで機能しましたが、何らかの理由でエラーが発生しています

Uncaught TypeError: Cannot call method 'hasOwnProperty' of null

これが私が使用しているスクリプトです

/* Install Dummy Data */
function install_dummy() {
    $('#install_dummy').click(function(){
    $.ajax({
            type: "post",
            url: $AjaxUrl,
            dataType: 'json',
            data: {action: "install_dummy", _ajax_nonce: $ajaxNonce},
            beforeSend: function() {
                $(".install_dummy_result").html('');
                $(".install_dummy_loading").css({display:""});
                $(".install_dummy_result").html("Importing dummy content...<br /> Please wait, this process can take up to a few minutes.");                    
            }, 
            success: function(response){ 
                var dummy_result = $(".install_dummy_result");
                if(typeof response != 'undefined')
                {
                    if(response.hasOwnProperty('status'))
                    {
                        switch(response.status)
                        {
                            case 'success':
                                    dummy_result.html('Dummy Data import was successfully completed');
                                break;
                            case 'error':
                                    dummy_result.html('<span style="color:#f00">'+response.data+'</span>');
                                break;
                            default:
                                break;
                        }

                    }
                }
                $(".install_dummy_loading").css({display:"none"});
            }
        }); 

    return false;
    });
}
install_dummy();

どんな助けでも大歓迎です。

4

1 に答える 1

1
if(typeof response != 'undefined')

つまり、「'response' という変数はありますか?」ということです。それを関数パラメーターとして受け取っているため、という変数が存在responseします。

変数の存在は、それができないという意味ではありませんnull。ここでresponseは、定義されている/存在しますが、ですnull。そして、あなたが言うとき:

if(response.hasOwnProperty('status'))

null 値で呼び出そうとするとhasOwnProperty、その例外が発生します。次のことを行う必要があります。

if (response !== null && response.hasOwnProperty(..)) { ... }
于 2013-04-13T04:10:00.497 に答える