2017-08-06 108 views
1

我想和愛可信PUT請求,我得到這個我的行動裏面至今:請求Axios |反應過來,終極版

export function updateSettings(item) { 
    return dispatch => { 
    console.log(item) 
    return axios.put(`/locks`).then(response => { 
     console.log(response) 
    }) 
    } 
} 

當我CONSOLE.LOG item我可以看到所有的事情我已經在我的打字輸入框內的對象,但後來我得到了404。我知道我有這個URI。有誰知道如何解決這個問題?

回答

2

放置響應將需要一個對象發送。 put的正確axios是這樣的:

export function updateSettings(item) { 
    return dispatch => { 
     console.log(item) 
     return axios.put(`/locks`, item).then(response => { 
      console.log(response) 
     }) 
    } 
} 

這很可能是你爲什麼得到錯誤,因爲PUT的對象是未定義的。

您可以在下面的鏈接中查看此列表,以瞭解如何使用axios製作正確的請求。 Axios request methods

+1

我會給你一個upvote指出,但我仍然得到錯誤。 –

+0

錯誤說的是什麼?嘗試在'then'之後添加'.catch(error => console.log(error))',但它也可能是API端的錯誤,嘗試儘可能多地嘗試記錄何時和在哪裏以及爲什麼你會收到404回覆給你。 –

+0

這是錯誤:'PUT http:// localhost:3001/locks 404(Not Found)'。 –

1

A PUT請求需要資源的標識符(例如id)和要更新的有效負載。您似乎沒有確定要更新的資源,因此。

你需要一個ID項目這樣。

export function updateSettings(id, item) { 
    return dispatch => { 
    console.log(item) 
    return axios.put(`/locks/${id}`, item).then(response => { 
     console.log(response) 
    }) 
    } 
} 
+0

將'$添加到'axios.put(\'/ locks/$ {id} \',item)'這樣它就可以工作,只是複製粘貼,否則變量不會得到正確補充 –

+0

不知道羅蘭,謝謝。知道我只需要將id發送給我的操作,因爲此時它尚未定義。 –

+0

當然,您可以將答案作爲*接受*來幫助其他可能錯過它的人 – Rowland