我正在使用PHP,JS,Axios和Ajax為instagram之類的應用程序提供類似功能。我能夠正確啟動想要單擊以顯示帖子的特定a標簽的click事件處理程序。我在執行ajax調用后收到的響應將不允許我檢查響應的狀態。我從我的php文件中傳遞了$ response ['status'] =“ liked”,但是我通過ajax調用獲得的數據變量卻不正確。我首先使用jquery制作了該功能,但想使用axios來避免jquery框架大小帶來的較長加載時間。我試圖使用res.data ['status']選擇狀態,但這也不起作用。我似乎得到一個結合了json響應的數組。// this selects the exact like button for the post I want to like using // the data attribute idlet btn = document.querySelector("a.like");btn.addEventListener("click", function(e){ let postId = this.dataset.id; let link = this; console.log("test"); axios.post('ajax/likePost.php',{ postId : postId }) .then (function (res){ console.log(res); if (res.data == "liked") { console.log("we zitten ion de liked"); let likes = link.nextSibling.innerHTML; link.children.src = "images/liked.svg"; likes++; link.nextSibling.innerHTML=likes; } else { console.log("we zitten ion de niet liked"); let likes = link.nextSibling.innerHTML; link.children.src = "images/like.svg"; likes--; link.nextSibling.innerHTML=likes; } }) .catch(function (error) { console.log(error); }); e.preventDefault();});
在使用php進行ajax調用后,如何從axios獲得正確的響應輸出?
飲歌長嘯
2021-05-07 11:10:33