"Uncaught RangeError: Maximum call stack size exceeded at Dt (jquery.min.js:2)"



我是用django编程的新手,现在我被困在这个阶段,我必须将数据从js变量移动到django视图或其他东西。但目前,如果我试图通过数据从js到Django使用ajax post函数它说未捕获的范围错误。我不确定我在哪里犯了错误,但如果有人能帮助我,那将是非常有帮助的。真的很有帮助,拜托!!

错误信息:

Uncaught RangeError: Maximum call stack size exceeded at Dt (jquery.min.js:2)
<<p>脚本代码/strong>
<script>
var URL = "{% url 'textFromInputFile' %}";
var textOfFile = document.getElementById('fileinput');      

textOfFile.addEventListener('change', function(){

var fr = new FileReader();
fr.onload = function(){
document.getElementById("textarea").value = fr.result;
};
fr.readAsText(this.files[0]);

});            

function getText(){  

$.ajax({
type: "POST",
url: "/textFromInputFile",
data: {"textOfFile":textOfFile},
dataType: "String",
success: function(data){
alert("ok")
},
failure:function(){
alert("failed")
}
},);}          


$('button').click(function(){
getText();
});
</script>

views.py

def textFromInputFile(request):
if request.method == 'POST':
if 'textOfFile' in request.POST:
textOfFile = request.POST['textOfFile']
#need to do something here
return HttpResponse('success')  #if everything is o.k
else:
return HttpResponse('failed!!')

urls . py

urlpatterns = [
path('', views.index, name='index'),
path('signin.html', views.signin, name='signin'),
path('index.html', views.index, name='index'),
path('home.html', views.home, name='home'),
path('logoutPage.html', views.logout, name='logout'),
path('home.html', views.textFromInputFile, name='textFromInputFile'),

)

这个错误可能是由于Ajax多次尝试序列化JSON造成的。所以我们最好在使用JSON发送之前序列化它。stringify ()尝试更改ajax函数,数据如下:

data:JSON.stringify({"textOfFile":textOfFile}),
另一种方法是将其添加到ajax函数中:
$.ajax({
type: "POST", url: "/textFromInputFile", 
data: {"textOfFile":textOfFile},
dataType: "String", 
success: function(data){ 
alert("ok") 
}, 
failure:function(){ 
alert("failed") 
},
cache: false,
contentType: false,
processData: false,
},);

最新更新