ajax的data參數(shù)錯誤導致頁面崩潰怎么辦

這篇文章主要介紹ajax的data參數(shù)錯誤導致頁面崩潰怎么辦,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

為古藺等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及古藺網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都做網(wǎng)站、網(wǎng)站設計、外貿(mào)營銷網(wǎng)站建設、古藺網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

今天準備把選定表格的其中一行的數(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ù)庫。

結果頁面不報錯,而是直接崩潰了,查看了許久,才發(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],
由于自己的粗心和對導致ajax出現(xiàn)錯誤的情況了解比較少,導致看了很久的代碼才發(fā)現(xiàn)原因,剛開始就以為不會是參數(shù)導致,
因為認為參數(shù)錯誤頂多拿到的數(shù)據(jù)不對,報下錯或者結果不一樣,果真自己還是太年輕。

以上是“ajax的data參數(shù)錯誤導致頁面崩潰怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章題目:ajax的data參數(shù)錯誤導致頁面崩潰怎么辦
標題路徑:http://muchs.cn/article24/jehoje.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、小程序開發(fā)、定制網(wǎng)站、品牌網(wǎng)站建設、虛擬主機網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設