您好,欢迎来到枫云教育。
搜索
您的当前位置:首页怎样解决ajax的data参数错误导致页面崩溃

怎样解决ajax的data参数错误导致页面崩溃

来源:枫云教育


这次给大家带来怎样解决ajax的data参数错误导致页面崩溃,解决ajax的data参数错误导致页面崩溃的注意事项有哪些,下面就是实战案例,一起来看一下。

代码如下:

$('.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),表单是数据来源于点击,然后修改表格
的内容点击确定按钮($('#ok'))后把表单数据覆盖掉之前点击行的数据,达到修改表格的目的,点击确定时触发ajax,
把修改后的数据发送给后端,拿到数据并更新数据库。

结果页面不报错,而是直接崩溃了,查看了许久,才发现是由于ajax的data参数写错了,之前写成这样:

 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",
 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';
 });

从上面可以看出,我传给data的数据并不是字符串之类的,而是一个n.fn.init [td, prevObject: n.fn.init(1), context: document],
由于自己的粗心和对导致ajax出现错误的情况了解比较少,导致看了很久的代码才发现原因,刚开始就以为不会是参数导致,
因为认为参数错误顶多拿到的数据不对,报下错或者结果不一样,果真自己还是太年轻。

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

怎样实现Vue项目中使用Vux

如何利用Vue中slot插槽分发父组件

Copyright © 2019- fengyunshixun.cn 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务