亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試檢查 if 語句中的 json 響應時出現未捕獲(承諾)錯誤

嘗試檢查 if 語句中的 json 響應時出現未捕獲(承諾)錯誤

暮色呼如 2022-10-21 10:26:24
我將數據作為 JSON 對象,如果數據 JSON 對象中的 id 與 buttonId 的 id 匹配,我想將數據元素推送到 jsonArray 中。然后我想發送到模態的 innerHTML 進行顯示。數據得到響應:[{"id":"4","task_detail":"Use online reports to gather data, confirm with manager and push client data back to Github."},{"id":"6","task_detail":"Pull client data, analyse and push back"},{"id":"9","task_detail":"Perms and user roles in db need creating"},{"id":"10","task_detail":"Pull expense data into API JSON the graph with AJAX and Chart JS"},{"id":"11","task_detail":"Left Side Navigation, requires BS and CSS Style"},{"id":"12","task_detail":"CSS Pipeline color scheme"},{"id":"13","task_detail":"Pull from db and display"},{"id":"14","task_detail":"Export to Excel for tables in reports"},{"id":"15","task_detail":"Test and come up with report data\/ideas to complete"},{"id":"16","task_detail":"Sort by status and date created"},{"id":"17","task_detail":"Add date created to the pipeline table"},{"id":"18","task_detail":"Display info"},{"id":"19","task_detail":"Add option for user to change details - password"},{"id":"20","task_detail":"Collapse from Bootstrap"},{"id":"21","task_detail":"After complete with 1, mimic to 2-5, update project.php buttons"},{"id":"22","task_detail":"Use alert or modal viewer to check if user if sure to delete, once btn pressed"}]錯誤:409 未捕獲(承諾中)類型錯誤:無法在 handleJsonData 讀取未定義的屬性“id”用于線  if (arrData[i].id == buttonId) {const pipe_api_url = 'http://localhost/site/handler.php';  var buttonId;  var taskDetail;  var jsonArray = [];  const data = [];  var stringData = [];  async function handleJsonData() {      const response = await fetch(pipe_api_url);    const data = await response.json();    var stringData = JSON.stringify(data);    console.log("Data: "+data);    console.log("stringData: "+stringData);    var hrefurl = window.location.href;    console.log("handleJsonData hrefurl: "+hrefurl);    var btnIndex = hrefurl.indexOf("btnId=");    console.log("handleJsonData btnIndex: "+btnIndex); //index 49 at currently
查看完整描述

1 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

這是適合您的工作代碼。

你變得未定義的原因data[i].id是你沒有遍歷你的data響應數組

我重新創建了一些 HTML 并添加了靜態定義的response=以重新創建工作代碼。data

您可以看到我正在forEach()處理數據并檢查是否buttonIddata.id

它增加了task_details四次jsonArray,我不知道你為什么button.length還要循環,所以我會把那個留給你。

工作演示: https ://jsfiddle.net/usmanmunir/eros9puf/31/

運行下面的代碼片段以查看它的工作原理

const pipe_api_url = 'http://localhost/site/handler.php';

var buttonId;

var taskDetail;

var jsonArray = [];

const data = [];

var stringData = [];


async function handleJsonData() {


  //const response = await fetch(pipe_api_url);

  const data = [{

    "id": "4",

    "task_detail": "Use online reports to gather data, confirm with manager and push client data back to Github."

  }, {

    "id": "6",

    "task_detail": "Pull client data, analyse and push back"

  }, {

    "id": "9",

    "task_detail": "Perms and user roles in db need creating"

  }, {

    "id": "10",

    "task_detail": "Pull expense data into API JSON the graph with AJAX and Chart JS"

  }, {

    "id": "11",

    "task_detail": "Left Side Navigation, requires BS and CSS Style"

  }, {

    "id": "12",

    "task_detail": "CSS Pipeline color scheme"

  }, {

    "id": "13",

    "task_detail": "Pull from db and display"

  }, {

    "id": "14",

    "task_detail": "Export to Excel for tables in reports"

  }, {

    "id": "15",

    "task_detail": "Test and come up with report data\/ideas to complete"

  }, {

    "id": "16",

    "task_detail": "Sort by status and date created"

  }, {

    "id": "17",

    "task_detail": "Add date created to the pipeline table"

  }, {

    "id": "18",

    "task_detail": "Display info"

  }, {

    "id": "19",

    "task_detail": "Add option for user to change details - password"

  }, {

    "id": "20",

    "task_detail": "Collapse from Bootstrap"

  }, {

    "id": "21",

    "task_detail": "After complete with 1, mimic to 2-5, update project.php buttons"

  }, {

    "id": "22",

    "task_detail": "Use alert or modal viewer to check if user if sure to delete, once btn pressed"

  }]

  var stringData = JSON.stringify(data);

  //console.log("Data: "+data);

  //console.log("stringData: "+stringData);


  var hrefurl = window.location.href;

  //console.log("handleJsonData hrefurl: "+hrefurl);


  var btnIndex = hrefurl.indexOf("btnId=1");

  //console.log("handleJsonData btnIndex: "+btnIndex); //index 49 at currently



  var startOfurlSlice = btnIndex + 6;

  var endOfUrlSlice = btnIndex.length;


  var slicedHrefUrl = hrefurl.slice(startOfurlSlice, endOfUrlSlice);

  //console.log("handleJsonData slicedHrefUrl: "+slicedHrefUrl);

  //var buttonId = slicedHrefUrl;


  var buttonId = 4;


  for (i = 0; i <= buttonId; i++) {

    data.forEach(function(data) {

      if (data.id == buttonId) {

        //jsonArray.push(data[0].id);

        jsonArray.push(data.task_detail);

        //console.log("handleJsonData jsonArray " + jsonArray);

      }

    })

  }

  document.getElementById("show-task-details").innerHTML = jsonArray;

}

$("button").click(async function() {

  buttonId = this.id; // or alert($(this).attr('id'));

  //window.location.href = "http://localhost/site/handler.php?btnId=" + buttonId;

  document.getElementById("modalLabelPipeDetail").innerHTML = "Details #" + buttonId;

  handleJsonData();

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="4">Click ME - ID = 4</button>


<div id="modalLabelPipeDetail"></div>


<div id="show-task-details"></div>


查看完整回答
反對 回復 2022-10-21
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號