簡體   English   中英

未捕獲的類型錯誤:無法讀取未定義的屬性“文檔”

[英]Uncaught TypeError: Cannot read property 'document' of undefined

我有以下功能,它在我測試過的幾台 PC 上運行良好。 我已經在 Chrome、IE 和 Firefox 上測試過,沒有任何問題。 但是,有 1 台特定的 PC(運行 Chrome)會在行上引發此錯誤“Uncaught TypeError: Cannot read property 'document' of undefined”:

                win.document.write(data);

可能是因為 win 為空?

如果是這樣,為什么在這台特定的 PC 上會出現這種情況?

是否有一些 Chrome 設置需要設置?

方法:

    function viewReport() {
        console.info('generating event report');
        var frmData = $('#frmEventReport').serializeArray();
        var rptName = 'EventReport' + Math.floor((Math.random() * 100) + 1);
        console.info('generated random report name ' + rptName);
        $.ajax({
            //type: "GET",
            timeout: 120000,
            url: '@Url.Action("EventReport", "Reports")',
            data: frmData,
            success: function (data) {
                console.info('succesfully called back');
                var win = window.open('', rptName, '_blank');
                console.info('opening window');
                win.document.write(data);

            },
            error: function (x, y, z) {
                console.info(x + ' ' + y + ' ' + z);
            }
        });
    }

該 PC 的 Chrome 是否啟用了彈出窗口? 如果不是,則無法創建新窗口,因此win undefined

這是因為當您嘗試創建一個彈出式異步Ajax請求成功后win得到undefined 所以你可以添加async:false如下,這對我有用:

function viewReport() {
    console.info('generating event report');
    var frmData = $('#frmEventReport').serializeArray();
    var rptName = 'EventReport' + Math.floor((Math.random() * 100) + 1);
    console.info('generated random report name ' + rptName);
    $.ajax({
        //type: "GET",
        timeout: 120000,
        url: '@Url.Action("EventReport", "Reports")',
        data: frmData,
        async:false,
        success: function (data) {
            console.info('succesfully called back');
            var win = window.open('', rptName, '_blank');
            console.info('opening window');
            win.document.write(data);

        },
        error: function (x, y, z) {
            console.info(x + ' ' + y + ' ' + z);
        }
    });
}

出現此錯誤是因為系統中禁用了彈出窗口。因此,通過系統設置選項啟用彈出窗口。

我具有以下功能,該功能可以在經過測試的幾台PC上正常運行。 我已經在Chrome,IE和Firefox上進行了測試,沒有任何問題。 但是,有1台特定的PC(運行Chrome),在行上拋出此錯誤“ Uncaught TypeError:Can not read property'document'of undefined”的行:

                win.document.write(data);

可能是因為勝利為空嗎?

如果是這樣,為什么在此特定PC上會出現這種情況?

是否需要設置一些Chrome設置?

方法:

    function viewReport() {
        console.info('generating event report');
        var frmData = $('#frmEventReport').serializeArray();
        var rptName = 'EventReport' + Math.floor((Math.random() * 100) + 1);
        console.info('generated random report name ' + rptName);
        $.ajax({
            //type: "GET",
            timeout: 120000,
            url: '@Url.Action("EventReport", "Reports")',
            data: frmData,
            success: function (data) {
                console.info('succesfully called back');
                var win = window.open('', rptName, '_blank');
                console.info('opening window');
                win.document.write(data);

            },
            error: function (x, y, z) {
                console.info(x + ' ' + y + ' ' + z);
            }
        });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM