我沒有很多 JS 經驗,但我試圖在滑鼠懸停事件上呼叫 API,以便我可以在 Bootstrap 彈出視窗中顯示一些用戶資訊。
我快到了,但是我的彈出視窗沒有顯示,因為在 Ajax 代碼完成后根本沒有呼叫該函式。
function getUserProfile(email){
return $.ajax({
url: my_url email,
method: 'GET',
data: data,
success: function (data){
console.log(data); // this works ok and we get data back
return data; // we get to here ok
},
async: true
});
}
$(".my-link-popup").off('mouseover').on('mouseover', function (){
getUserProfile(this.href).then(
function(x){
console.log(x) // we don't get to here at all
$(this).popover({
html: true,
title: "<span>x.displayName</span>",
content: "Some nice content"
});
$(this).popover('show');
}
)
})
我哪里錯了?
uj5u.com熱心網友回復:
移除成功回呼:
function getUserProfile(email) {
return $.ajax({
url: my_url user_email,
method: 'GET',
data: data // <- where the data part is coming from
});
}
注意:ajax請求中缺少資料部分
uj5u.com熱心網友回復:
最后看起來該函式正在運行,但它不喜歡 $(this)。
這有效:
function getUserProfile(email){
return $.ajax({
url: my_url email,
method: 'GET',
data: data,
success: function (data){
console.log(data); // this works ok and we get data back
return data; // we get to here ok
},
async: true
});
}
$(".my-link-popup").off('mouseover').on('mouseover', function (){
let context = this; //added this line
getUserProfile(this.href).then(
function(x){
console.log(x)
$(context).popover({
html: true,
title: "<span>x.displayName</span>",
content: "Some nice content"
});
$(context).popover('show');
}
)
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/357760.html
標籤:javascript 查询
上一篇:如何在時間線上確定某事是否開始
