ajax的data参数错误导致页面崩溃
发布时间:2020-12-16 03:11:13 所属栏目:百科 来源:网络整理
导读:今天准备把选定表格的其中一行的数据通过ajax传给后端,但是网站确崩溃了。 代码如下: $('.icon-edit').click(function (event) { 这是一个按钮 o=$('.icon-edit').index($(this))+1; edit.style.display='block'; //console.log('$(this)',$(this).parent(
今天准备把选定表格的其中一行的数据通过ajax传给后端,但是网站确崩溃了。 代码如下:$('.icon-edit').click(function (event) { 这是一个按钮 o=$('.icon-edit').index($(this))+1; edit.style.display='block'; //console.log('$(this)',$(this).parent().parent()); let message=$(this).parent().parent(); $("#non").val(message.children('td:eq(0)').html()); $("#name").val(message.children('td:eq(1)').html()); $("#sex").val(message.children('td:eq(2)').html()); $("#age").val(message.children('td:eq(3)').html()); $("#xueyuan").val(message.children('td:eq(4)').html()); $("#grade").val(message.children('td:eq(5)').html()); $("#phone").val(message.children('td:eq(6)').html()); $("#room").val(message.children('td:eq(7)').html()); l=message.children('td:eq(0)').html(); }); $('#ok').click(function () { //event.stopImmediatePropagation(); let text=$('table'); id=$('#non').val(); username=$('#name').val(); sex=$('#sex').val(); age=$('#age').val(); institute=$('#xueyuan').val(); grade=$('#grade').val(); phone=$('#phone').val(); hlbhl=$('#room').val() text.find("tr:eq("+o+")").children('td:eq(0)').text(id); text.find("tr:eq("+o+")").children('td:eq(1)').text(username); text.find("tr:eq("+o+")").children('td:eq(2)').text(sex); text.find("tr:eq("+o+")").children('td:eq(3)').text(age); text.find("tr:eq("+o+")").children('td:eq(4)').text(institute); text.find("tr:eq("+o+")").children('td:eq(5)').text(grade); text.find("tr:eq("+o+")").children('td:eq(6)').text(phone); text.find("tr:eq("+o+")").children('td:eq(7)').text(hlbhl); $.ajax({ type: "POST",url: "doAction2.php",//请求的后台地址 data: { non:o,id: id,username: username,sex: sex,age: age,institute: institute,grade: grade,phone: phone,hlbhl: hlbhl },//前台传给后台的参数 dataType: "json",ansync: true,ContentType: "application/json; charset=utf-8",success: function (msg) {//msg:返回值 a=2; console.log(a); } }); edit.style.display='none'; }); 代码的大意是我点击一个按钮($('.icon-edit'))然后弹出一个表单(edit),表单是数据来源于点击,然后修改表格 id=text.find("tr:eq("+o+")").children('td:eq(0)').text(id); username=text.find("tr:eq("+o+")").children('td:eq(1)').text(username); sex=text.find("tr:eq("+o+")").children('td:eq(2)').text(sex); age=text.find("tr:eq("+o+")").children('td:eq(3)').text(age); institute=text.find("tr:eq("+o+")").children('td:eq(4)').text(institute); grade=text.find("tr:eq("+o+")").children('td:eq(5)').text(grade); phone=text.find("tr:eq("+o+")").children('td:eq(6)').text(phone); hlbhl=text.find("tr:eq("+o+")").children('td:eq(7)').text(hlbhl); $.ajax({ type: "POST",success: function (msg) {//msg:返回值 a=2; console.log(a); } }); edit.style.display='none'; }); 从上面可以看出,我传给data的数据并不是字符串之类的,而是一个n.fn.init[td,prevObject: n.fn.init(1),context: document],由于自己的粗心和对导致ajax出现错误的情况了解比较少,导致看了很久的代码才发现原因,刚开始就以为不会是参数导致,因为认为参数错误顶多拿到的数据不对,报下错或者结果不一样,果真自己还是太年轻。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |