gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'removeChild' of null without using removeChild

转载 作者:行者123 更新时间:2023-11-29 10:57:55 25 4
gpt4 key购买 nike

我不使用 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();
}
})
})

最佳答案

查看源代码,我发现了如何设置 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 方法的参数。这实际上是您可以在 ajaxSetup 中找到的 text 参数。

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

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

更新:

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

您的响应中还有以下 html 导致了问题:

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

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

关于javascript - 未捕获的类型错误 : Cannot read property 'removeChild' of null without using removeChild,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53458658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com