詳解ajax的data參數(shù)錯(cuò)誤導(dǎo)致頁(yè)面崩潰
今天準(zhǔn)備把選定表格的其中一行的數(shù)據(jù)通過(guò)ajax傳給后端,但是網(wǎng)站確崩潰了。
代碼如下:
$('.icon-edit').click(function (event) { 這是一個(gè)按鈕 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",//請(qǐng)求的后臺(tái)地址 data: { non:o, id: id, username: username, sex: sex, age: age, institute: institute, grade: grade, phone: phone, hlbhl: hlbhl },//前臺(tái)傳給后臺(tái)的參數(shù) dataType: "json", ansync: true, ContentType: "application/json; charset=utf-8", success: function (msg) {//msg:返回值 a=2; console.log(a); } }); edit.style.display='none'; });
代碼的大意是我點(diǎn)擊一個(gè)按鈕($('.icon-edit'))然后彈出一個(gè)表單(edit),表單是數(shù)據(jù)來(lái)源于點(diǎn)擊,然后修改表格
的內(nèi)容點(diǎn)擊確定按鈕($('#ok'))后把表單數(shù)據(jù)覆蓋掉之前點(diǎn)擊行的數(shù)據(jù),達(dá)到修改表格的目的,點(diǎn)擊確定時(shí)觸發(fā)ajax,
把修改后的數(shù)據(jù)發(fā)送給后端,拿到數(shù)據(jù)并更新數(shù)據(jù)庫(kù)。
結(jié)果頁(yè)面不報(bào)錯(cuò),而是直接崩潰了,查看了許久,才發(fā)現(xiàn)是由于ajax的data參數(shù)寫錯(cuò)了,之前寫成這樣:
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",//請(qǐng)求的后臺(tái)地址 data: { non:o, id: id, username: username, sex: sex, age: age, institute: institute, grade: grade, phone: phone, hlbhl: hlbhl },//前臺(tái)傳給后臺(tái)的參數(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ù)并不是字符串之類的,而是一個(gè)n.fn.init [td, prevObject: n.fn.init(1), context: document],
由于自己的粗心和對(duì)導(dǎo)致ajax出現(xiàn)錯(cuò)誤的情況了解比較少,導(dǎo)致看了很久的代碼才發(fā)現(xiàn)原因,剛開(kāi)始就以為不會(huì)是參數(shù)導(dǎo)致,
因?yàn)檎J(rèn)為參數(shù)錯(cuò)誤頂多拿到的數(shù)據(jù)不對(duì),報(bào)下錯(cuò)或者結(jié)果不一樣,果真自己還是太年輕。
- 快速解決ajax請(qǐng)求出錯(cuò)狀態(tài)碼為0的問(wèn)題
- PHP 中使用ajax時(shí)一些常見(jiàn)錯(cuò)誤總結(jié)整理
- 完美解決ajax跨域請(qǐng)求下parsererror的錯(cuò)誤
- ajax跨域訪問(wèn)報(bào)錯(cuò)501的解決方法
- jQuery中ajax錯(cuò)誤調(diào)試分析
- Ajax向后臺(tái)傳json格式的數(shù)據(jù)出現(xiàn)415錯(cuò)誤的原因分析及解決方法
- Ajax犯的錯(cuò)誤處理方法
- js ajaxfileupload.js上傳報(bào)錯(cuò)的解決方法
- 解決ajax返回驗(yàn)證的時(shí)候總是彈出error錯(cuò)誤的方法
- Jquery Ajax Error 調(diào)試錯(cuò)誤的技巧
- django使用ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤如何解決
- Ajax報(bào)錯(cuò)400的參考解決辦法
相關(guān)文章
JQuery實(shí)現(xiàn)簡(jiǎn)單瀑布流布局
這篇文章主要為大家詳細(xì)介紹了JQuery實(shí)現(xiàn)簡(jiǎn)單瀑布流布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07js之ActiveX控件使用說(shuō)明 new ActiveXObject()
ActiveX 控件廣泛用于Internet。它們可以通過(guò)提供視頻、動(dòng)畫內(nèi)容等來(lái)增加瀏覽的樂(lè)趣。不過(guò),這些程序可能出問(wèn)題或者向您提供不需要的內(nèi)容2014-03-03jquery slibings選取同級(jí)其他元素的實(shí)現(xiàn)代碼
jquery選取同級(jí)其他元素可以使用slibings方法,end方法可以清除之前的鏈?zhǔn)讲僮鳎喈?dāng)于重新開(kāi)始2013-11-11基于Jquery和html5實(shí)現(xiàn)炫酷的3D焦點(diǎn)圖動(dòng)畫
這篇文章主要介紹了基于Jquery和html5實(shí)現(xiàn)炫酷的3D焦點(diǎn)圖動(dòng)畫的相關(guān)資料,焦點(diǎn)圖中的圖片利用了CSS3的相關(guān)特性實(shí)現(xiàn)圖片傾斜效果,從而讓圖片出現(xiàn)3D的視覺(jué)效果,感興趣的小伙伴們可以參考一下2016-03-03jquery tools系列 expose 學(xué)習(xí)
不知不覺(jué),已經(jīng)進(jìn)行到了jquery tools六大功能的第五個(gè)功能——expose的學(xué)習(xí)了,廢話不多說(shuō),直接進(jìn)入正題。2009-09-09用jQuery.ajaxSetup實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)數(shù)據(jù)的過(guò)濾
本文主要對(duì)用jQuery.ajaxSetup實(shí)現(xiàn)對(duì)請(qǐng)求和響應(yīng)數(shù)據(jù)的過(guò)濾的過(guò)程與方法進(jìn)行詳細(xì)全面的實(shí)例講解。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12jQuery經(jīng)過(guò)一段時(shí)間自動(dòng)隱藏指定元素的方法
這篇文章主要介紹了jQuery經(jīng)過(guò)一段時(shí)間自動(dòng)隱藏指定元素的方法,實(shí)例分析了兩種比較常用的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03jQuery模擬原生態(tài)App上拉刷新下拉加載更多頁(yè)面及原理
很多App中,新聞或者展示類都存在下拉刷新和上拉加載的效果,如何實(shí)現(xiàn)上拉刷新下拉加載更多頁(yè)面的呢?下面小編通過(guò)下面內(nèi)容給大家介紹jQuery模擬原生態(tài)App上拉刷新下拉加載更多頁(yè)面及原理,需要的朋友可以參考下2015-08-08