我有一個<img>這樣的元素:
<img v-bind:word = "thing" @click="action" align="center" src="../assets/pic.png"/>
和這樣的方法:
async action() {
let imgAttribute = event.target.getAttribute('word');
alert(imgAttribute ); // alerts OK
console.log("imgAttribute : " imgAttribute ); // prints OK
await axios.post('http://localhost:3000/my/endpoint',
{
params: {
word: imgAttribute
}
});
...
因此,當<img>單擊元素時,將action執行該方法。獲取屬性并將其word放入imgAttribute變數中,然后在發布請求中將其作為引數傳遞。
alert(imgAttribute)并且console.log(imgAttribute)作業正常,這意味著它們不列印undefined,而是列印屬性內部的正確字串值word。
但是在 Express 服務器上:
app.post('/my/endpoint', async (req, res) => {
const {word} = req.params; // tried req.query with the same (undefined) result
console.log("word = " word); // undefined
...
word是未定義的。
為什么會這樣?我如何解決它?
uj5u.com熱心網友回復:
您通過 .post 作為正文傳遞,作為引數傳遞使用
await axios({
url: 'http://localhost:3000/my/endpoint',
method: 'post',
params: {
word: imgAttribute
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/428877.html
標籤:javascript Vue.js 表示
