javascript - jQuery.ajax 给出 "TypeError: Cannot read property ' documentElement' of null"on server but not local

标签 javascript jquery xml ajax

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

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

我检查并重新检查并重新上传了我能想到的所有内容。文档说要确保我发送的是正确的 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;
}

然后它继续将页面渲染到一个 div 中。它抓取的页面在前一个 url 的/ajax 文件夹中可见。如果您能提供任何帮助,我们将不胜感激!

最佳答案

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

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

由于您需要 XML 数据类型,因此对“ajax/home.html”url 的调用将返回一个 mime 类型“text/html”。对于 jQuery 的 intelligent guess,您可能希望完全省略数据类型。开始。例如:

$.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); }
});

您在 appendCSS 中还有以下 alert(),我认为它用于调试?:

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

关于javascript - jQuery.ajax 给出 "TypeError: Cannot read property ' documentElement' of null"on server but not local,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2189214/

相关文章:

javascript - 如果声明只考虑每小时

xml - 用 xquery 替换元素的值

javascript - 在 JavaScript 中将 XML 属性转换为 HTML 标签

javascript - 如何在 Angular 中基于一个 http 调用进行多次验证

javascript - 如何在 <li> 内设置自动间距,元素始终位于最右端

javascript - Meteor:如何对 session 变量的集合进行排序?

.net - 使用 DotNet 从 XML 字符串中删除已知位置的元素的最佳方法?

javascript - 延迟 ember.js 应用程序准备,直到初始 ajax 调用返回

javascript - Bootstrap 表单组复选框不会在导航丸之间被选中

javascript - 使用 jQuery : How to slice from an array and organize 3 rows (imgs. 长度/3) 组织 HTML "items",其中每个项目都有