我使用 ajax 呼叫從資料庫中檢索資料。在我的代碼中無法訪問parameter父 ajax 呼叫請求到子 ajax 請求呼叫。我可以在控制臺中看到錯誤。我可以嘗試放入asyc: false子請求,但使用async我可以訪問引數但子 ajax 呼叫只能作業一次。
這是我的代碼
$.ajax({
type: "GET",
url: "/url",
data: { acId: acId, pstId: pstId },
success: function(res) {
// res.lenth = 2
for (var i = 0; i < res.length; i ) {
$.ajax({
type: "GET",
url: "/url",
data: {commentId: res[i].comment_id},
success: function(likeornot){
if(likeornot == 0)
{
$("#btnlikeornot").append("<button type = 'submit' onclick ='btncommentunlike(" res[i].comment_id ")' id='commentunlikebtn" res[i].comment_id "' data-id='" res[i].u_id " ' class='comment-like-button-style'>"
"<img src='https://img.icons8.com/material-outlined/24/000000/like--v1.png'/>"
"</button>");
}
else
{
$("#btnlikeornot").append("<button type = 'submit' onclick ='btncommentlike(" res[i].comment_id ")' id='commentlikebtn" res[i].comment_id "' data-id='" res[i].u_id " ' class='comment-like-button-style'>"
"<img src='https://img.icons8.com/material-rounded/24/fa314a/like.png' />"
"</button>");
}
}
});
}
}
});
錯誤
imagezoom.js:68 Uncaught TypeError: Cannot read properties of undefined (reading 'comment_id')
at Object.success (imagezoom.js:68)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at A (jquery.min.js:4)
at XMLHttpRequest.<anonymous> (jquery.min.js:4)
uj5u.com熱心網友回復:
問題是在i成功事件發生之前變數發生了變化,所以它沒有預期的值。要解決這個問題,您必須為回圈的每次迭代使用變數的本地副本。這可以通過使用let而不是宣告變數來實作var:
for (let i = 0; i < res.length; i ) {
uj5u.com熱心網友回復:
我不知道物件 res,但這是您更喜歡在沒有任何異步行程的情況下在 ajax 中呼叫 ajax 的方式。沒有必要。
//First ajax: return comments
function func_GetComments() {
return new Promise(function (resolve, reject) {
$.ajax({
type: "GET",
url: "/meetzen/comments",
data: { acId: accountId, pstId: postId },
success: function (res) {
if (res.length > 0) {
resolve(res);
}
else {
reject("true");
}
}
});
});
}
//第二個ajax,讀取成功塊的評論。 資料:{commentId:comment.comment_id},這里使用comment物件獲取id
function GetLikeOrNotImgZoom(comment) {
$.ajax({
type: "GET",
url: "/meetzen/getlikeornotimgzoom",
data: { commentId: comment.comment_id },
success: function (likeornot) {
if (likeornot == 0) {
//i have chaned syntex to concat string, apply in else as well to better perfromace.
$("#btnlikeornot").append(`<button type = 'submit' onclick ='btncommentunlike("${res[i].comment_id}")' id="commentunlikebtn${res[i].comment_id}" data-id="${res[i].u_id}" class='comment-like-button-style'><img src='https://img.icons8.com/material-outlined/24/000000/like--v1.png'/></button>`);
}
else {
$("#btnlikeornot").append("<button type = 'submit' onclick ='btncommentlike(" res[i].comment_id ")' id='commentlikebtn" res[i].comment_id "' data-id='" res[i].u_id " ' class='comment-like-button-style'>"
"<img src='https://img.icons8.com/material-rounded/24/fa314a/like.png' />"
"</button>");
}
}
});
}
我們呼叫ajax的方式總是很重要的。
主要起始執行緒 參考承諾:https : //www.w3schools.com/js/tryit.asp?filename=tryjs_promise2
let IPromiseToReturnComments = func_GetComments();
IPromiseToReturnComments.then(() => {
//here value is comments which is return by func_GetComments promise.
$.each(value, (index, comment) => {
GetLikeOrNotImgZoom(comment);
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376473.html
