[英]Uncaught TypeError: Cannot read property 'appendChild' of undefined after Ajax call
I wanted to replace my html with the new Ajax response html but this error shows in the console and my script files are not loading correctly. 我想用新的Ajax响应html替换html,但是此错误在控制台中显示,并且我的脚本文件未正确加载。 Apparently this error comes in the file "jquery-1.10.1.min.js".
显然,此错误来自文件“ jquery-1.10.1.min.js”。 Following is my ajax code which I used to replace with the html in the success function.
以下是我的ajax代码,我曾经在成功函数中用html替换过。
$(function() {
$('#addcustomer-done').click(function() {
var formData = new FormData($('#submit_form')[0]);
//---------------------- AJAX CALL 1 -----------------------------------------//
$.ajax({
// headers : {
// 'X-CSRF-Token' : document.getElementsByName('csrfmiddlewaretoken')[0].value
// },
url : "/savecustomer/",
type : "POST",
data:formData,
contentType: false,
cache: false,
processData: false,
async: false,
cache:false,
contentType: false,
processData: false,
success : function(data) {
alert(data);
$("html").html("");
$("html").html(data);
}
I'm a beginner at ajax. 我是ajax的初学者。 Kindly guide me how to get rid of this error.
请指导我如何摆脱这个错误。 Thanks in advance.
提前致谢。
Dont clear the elements inside html tags. 不要清除html标记内的元素。 Use an element present in the dom to append the value.
使用dom中存在的元素来附加值。 Also remove
async:false
from your ajax. 还要从ajax中删除
async:false
。 Because ajax should run asynchronously. 因为ajax应该异步运行。
$(function() {
$('#addcustomer-done').click(function() {
var formData = new FormData($('#submit_form')[0]);
$.ajax({
url: "/savecustomer/",
type: "POST",
data: formData,
success: function(data) {
console.log(data);
$("#id of element inside tab").append(data);
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.