詳解ajax的data參數(shù)錯誤導(dǎo)致頁面崩潰
今天準(zhǔn)備把選定表格的其中一行的數(shù)據(jù)通過ajax傳給后端,但是網(wǎng)站確崩潰了。
代碼如下:
$('.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
},//前臺傳給后臺的參數(shù)
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),表單是數(shù)據(jù)來源于點擊,然后修改表格
的內(nèi)容點擊確定按鈕($('#ok'))后把表單數(shù)據(jù)覆蓋掉之前點擊行的數(shù)據(jù),達到修改表格的目的,點擊確定時觸發(fā)ajax,
把修改后的數(shù)據(jù)發(fā)送給后端,拿到數(shù)據(jù)并更新數(shù)據(jù)庫。
結(jié)果頁面不報錯,而是直接崩潰了,查看了許久,才發(fā)現(xiàn)是由于ajax的data參數(shù)寫錯了,之前寫成這樣:
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
},//前臺傳給后臺的參數(shù)
dataType: "json",
ansync: true,
ContentType: "application/json; charset=utf-8",
success: function (msg) {//msg:返回值
a=2;
console.log(a);
}
});
edit.style.display='none';
});
從上面可以看出,我傳給data的數(shù)據(jù)并不是字符串之類的,而是一個n.fn.init [td, prevObject: n.fn.init(1), context: document],
由于自己的粗心和對導(dǎo)致ajax出現(xiàn)錯誤的情況了解比較少,導(dǎo)致看了很久的代碼才發(fā)現(xiàn)原因,剛開始就以為不會是參數(shù)導(dǎo)致,
因為認為參數(shù)錯誤頂多拿到的數(shù)據(jù)不對,報下錯或者結(jié)果不一樣,果真自己還是太年輕。
- 快速解決ajax請求出錯狀態(tài)碼為0的問題
- PHP 中使用ajax時一些常見錯誤總結(jié)整理
- 完美解決ajax跨域請求下parsererror的錯誤
- ajax跨域訪問報錯501的解決方法
- jQuery中ajax錯誤調(diào)試分析
- Ajax向后臺傳json格式的數(shù)據(jù)出現(xiàn)415錯誤的原因分析及解決方法
- Ajax犯的錯誤處理方法
- js ajaxfileupload.js上傳報錯的解決方法
- 解決ajax返回驗證的時候總是彈出error錯誤的方法
- Jquery Ajax Error 調(diào)試錯誤的技巧
- django使用ajax post數(shù)據(jù)出現(xiàn)403錯誤如何解決
- Ajax報錯400的參考解決辦法
相關(guān)文章
jQuery插件pagination實現(xiàn)分頁特效
jQuery分頁插件示例,ajax方式的jQuery前端分頁插件,其核心代碼jquery.pagination.js已隨示例打包,Demo中包括了5種不同的分頁效果演示,全部是Ajax無刷新方式實現(xiàn)分頁,對于搞前端開發(fā)的朋友來說,這是個值得收藏的代碼特效,為以后實現(xiàn)無刷新分頁提供方便。2015-04-04
Raphael一個用于在網(wǎng)頁中繪制矢量圖形的Javascript庫
Raphael是一個用于在網(wǎng)頁中繪制矢量圖形的Javascript庫,它使用 SVG W3C 推薦標(biāo)準(zhǔn)和 VML 作為創(chuàng)建圖形的基礎(chǔ),你可以通過 JavaScript 操作 DOM 來輕松創(chuàng)建出各種復(fù)雜的柱狀圖、餅圖、曲線圖等各種圖表,接下來詳細介紹,感興趣的朋友可以了解下哦2013-01-01
jQuery實現(xiàn)提交按鈕點擊后變成正在處理字樣并禁止點擊的方法
這篇文章主要介紹了jQuery實現(xiàn)提交按鈕點擊后變成正在處理字樣并禁止點擊的方法,涉及jQuery中val與attr方法的使用技巧,需要的朋友可以參考下2015-03-03
基于jquery的loading 加載提示效果實現(xiàn)代碼
有時候為了更好的用戶體驗,使用jquery的朋友可以參考下代碼。2011-09-09

