iT邦幫忙

0

問題已解決-axios delete 請求回來 response console.log()有內容,但讀取不到

串接後端API,發delete的requestconsole.log(res) 回來有內容,以為是型別問題,但發現res有內容,但讀取不到,有沒有大大有類似經驗可以分享一下

const courseRequest = axios.create({
    baseURL: `https://search`,
});
export const apiDeleteRecentSearch = (url) => courseRequest.delete(url);
   async function deleteAllRecentSearch() {
      isSearchFocus.value = true;
      const res = await apiDeleteRecentSearch(
        `recentSearch?empNum=${memberID.value}`
      );
      console.log('res :>> ', res);
      console.log('res.data.success :>> ', typeof res.data.success);
      console.log('res.data.keywords :>> ', typeof res.data.keywords);
      console.log('res.success :>> ', typeof res.data.success);
      console.log('res.keywords :>> ', typeof res.data.keywords);
      if (res.success === true) {
        console.log('刪除全部成功');
        recentSearchArray.value = res.keywords;
      }
    }

https://ithelp.ithome.com.tw/upload/images/20211126/20124879GPPSwMnhCm.png

---問題已解決---
經過昨天的測試發現是後端那裡給的格式有問題,用postman才發現res.data內的內容多了:
謝謝大家熱心地回覆!!

為何??你會覺得有內容??
從你的記錄看。都是無效值啊?

你要不要先直接看你的RES收到什麼了。
謝謝大大的回覆,昨天測試才發現是後端格式問題
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2021-11-26 22:04:36

console.log('res.data :>> ', res.data);看看會有什麼結果

看更多先前的回應...收起先前的回應...
fillano iT邦超人 1 級 ‧ 2021-11-27 08:54:21 檢舉

猜測:console.log(typeof res.data),結果會是string

fillano iT邦超人 1 級 ‧ 2021-11-27 09:55:26 檢舉

經驗談,要檢查res.data.success,那應該從上而下檢查res => res.data => res.data.success。

有 res, typeof res.data.success ,就是缺了 res.data /images/emoticon/emoticon10.gif

console.log('res :>> ', res);
console.log('res.data.success :>> ', typeofres.data.success);

fillano倒是提醒了我一件事。
會不會他其實拿到的,並不是JSON的值。而是JSON格式的字串值?
因為我以前曾經犯這個蠢事過。
用 console 看到有值(其實是字串值,但一時之間還沒意識過來),但物件取值取不到。
找原因找到快瘋潰。最後才意識到,我忘了輸出宣告JSON格式。

照這張圖來看是有 application/JSON

不過樓主發問之後都不知道雲遊何方
我們也不必在這裡一頭熱就是了
/images/emoticon/emoticon66.gif

謝謝大大們的回覆,昨天測試發現是後端格式錯誤

我要發表回答

立即登入回答