【问题标题】:Uncaught TypeError: Cannot read property 'removeChild' of null without using removeChild未捕获的类型错误:如果不使用 removeChild,则无法读取 null 的属性“removeChild”
【发布时间】:2018-11-24 13:32:23
【问题描述】:

我不使用removeChild(),也没有尝试删除之前的东西。

jquery-3.3.1.min.js:2 Uncaught TypeError: Cannot read property 'removeChild' of null
    at m (jquery-3.3.1.min.js:2)
    at Function.globalEval (jquery-3.3.1.min.js:2)
    at text script (jquery-3.3.1.min.js:2)
    at Ut (jquery-3.3.1.min.js:2)
    at k (jquery-3.3.1.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery-3.3.1.min.js:2)
    at Object.send (jquery-3.3.1.min.js:2)
    at Function.ajax (jquery-3.3.1.min.js:2)
    at Function.w._evalUrl (jquery-3.3.1.min.js:2)
    at Re (jquery-3.3.1.min.js:2)

我收到$.ajax html 并.html() 将它放入一个 div 容器中。

$(".kontakt").click(function(a) {
      $('.opt-out').fadeOut();
      a.stopPropagation()
      a.preventDefault()

      $.ajax({async: true,
      type: "POST",
       url: "classes/handle.ajax.class.php",
       data: {
            class: "kontakt"
       },
       success: function(a) {
         $(".link-container").html(a)
         $('.link-container').fadeIn();
         blur();
       }
     })
})

【问题讨论】:

  • javascript 在哪一行停止了?
  • @Mohammad javascript 没有停止。

标签: javascript jquery


【解决方案1】:

查看源代码,我发现 ajax 是如何设置的:

jQuery.ajaxSetup( {
    accepts: {
        script: "text/javascript, application/javascript, " +
            "application/ecmascript, application/x-ecmascript"
    },
    contents: {
        script: /\b(?:java|ecma)script\b/
    },
    converters: {
        "text script": function( text ) {
            jQuery.globalEval( text ); /* note to this method */
            return text;
        }
    }
} );

那么,让我们看看 globalEval 方法:

globalEval: function( code ) {
  DOMEval( code );
},

现在进入 DOMEval 方法:

function DOMEval( code, doc, node ) {
  doc = doc || document;

  var i,
    script = doc.createElement( "script" );

  script.text = code;
  if ( node ) {
    for ( i in preservedScriptAttributes ) {
      if ( node[ i ] ) {
        script[ i ] = node[ i ];
      }
    }
  }
  /* removeChild is being called */
  doc.head.appendChild( script ).parentNode.removeChild( script );
}

在哪里,您可能知道 parentNode 为 null,并且在 null 上调用 removeChild 会引发错误。在这里,在前面的代码中,您可以看到script 分配了text 属性,该属性的值设置为codecodeDOMEval 方法的参数。这实际上是 text 参数,您可以在 ajaxSetup 中找到。

ajaxSetup 正在被 ajax 方法调用(您可以查看源代码)。其中有 urloptions 参数。而在options 参数中,您可以使用find dataType 属性。如果没有设置dataType,它会猜测它的类型,服务器可能会返回null{}的响应。

所以,我建议您在使用jQuery.ajax 时设置正确的dataType。这应该可以解决您的问题。

更新:

好吧,如果服务器返回的响应格式不正确,那么它也可能会引发错误。因此,验证服务器是否返回了良好的响应。根据您的评论,我调试了代码,发现响应不是很好。

您的回复中还有以下 html,这也是导致问题的原因:

<head>
<meta name='robots' content='noindex'>
</head>

请将它们从响应源中删除,这样应该可以解决问题。

【讨论】:

  • 我已将它添加到我的 ajax 调用中,但它没有改变任何东西:/ $.ajax({async: true, type: "POST", url: "classes/handle.ajax.class.php", dataType: "text", data: { class: "kontakt" }, success: function(a) { $(".link-container").html(a) $('.link-container').fadeIn(); blur(); } })
  • 即使我使用 as ProcessData = html 也没有任何改变。奇怪的是在本地主机上我没有收到这个错误...
  • 也许网站链接会有所帮助。 photo.core-studio.de 当您点击“Datenschutz”、“Urheberschaft”和“Impressum”时会发生这种情况。不在“Kontakt”
猜你喜欢
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
  • 2021-11-18
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多