Javascript jQuery.ajax 在服务器上但不是本地给出“TypeError:无法读取 null 的属性‘documentElement’”

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

jQuery.ajax gives "TypeError: Cannot read property 'documentElement' of null" on server but not local

javascriptjqueryxmlajax

提问by vilhalmer

I'm having a problem with my jQuery code on http://alpha.spherecat1.com/, but the local copy works fine. As you can see if you visit the site now, the ajax call gives the following error:

我在http://alpha.spherecat1.com/上的 jQuery 代码有问题,但本地副本工作正常。如您所见,如果您现在访问该站点,ajax 调用会出现以下错误:

"TypeError: Cannot read property 'documentElement' of null"

“类型错误:无法读取 null 的属性‘documentElement’”

I've checked and rechecked and reuploaded everything I can think of. The documentation said to make sure I was sending the right MIME type, which I did to no avail. Here's the offending code:

我已经检查并重新检查并重新上传了我能想到的所有内容。文档说要确保我发送的是正确的 MIME 类型,但我这样做无济于事。这是违规代码:

function changePageAJAX(newPage, method)
{
    if (method != "replace")
    {
        window.location.hash = newPage;
    }
    newPage = newPage.substring(1); // Remove the hash symbol.
    title = "SphereCat1.com | " + fixCase(newPage.replace(/\//g, " > ")); // Set the window title.
    newPage = "ajax/" + newPage + ".html"; // Add path.
    if (newPage == currentPage)
    {
        return; // Don't let them load the current page again.
    }
    $.ajax({ // jQuery makes me feel like a code ninja.
        url: newPage,
        dataType: "xml",
        success: function(data, status, xmlhttp){ renderPage(xmlhttp); },
        error: function(xmlhttp, status, error){ alert(error); renderPage(xmlhttp); }
    });
    document.title = title;
    currentPage = newPage;
}

It then goes on to render the page into a div. The pages it's grabbing are visible in the /ajax folder at the previous url. Any help you can provide would be greatly appreciated!

然后它继续将页面呈现为一个 div。它抓取的页面在前一个 url 的 /ajax 文件夹中可见。您能提供的任何帮助将不胜感激!

回答by o.k.w

The error is thrown from the ajax call:

错误是从 ajax 调用中抛出的:

error: function(xmlhttp, status, error)...

Since you are expecting a XML dataType, the call for "ajax/home.html" url will return a mime type "text/html". You might want to omit the dataType totally for jQuery's intelligent guessto kick in. E.g.:

由于您期望使用 XML 数据类型,因此对“ajax/home.html” url 的调用将返回 MIME 类型“text/html”。您可能希望完全省略 dataType 以便 jQuery 的智能猜测开始。例如:

$.ajax({ // jQuery makes me feel like a code ninja.
  url: newPage,
  success: function(data, status, xmlhttp){ renderPage(xmlhttp); },
  error: function(xmlhttp, status, error){ alert(error); renderPage(xmlhttp); }
});

You also have the following alert()in appendCSSwhich I assume is for debugging?:

您也有以下alert()appendCSS,我认为是用于调试?:

function appendCSS(filename)
{
    alert(filename);
    if (filename != null)
    {
        $("head").append("<link rel='stylesheet' id='extracss' href='"+filename+"' type='text/css' />");
    }
}