1
我試着翻譯下面的jquery代碼來改爲使用獲取API。它使一個PUT
請求:
function save() {
$.ajax('/{{user.username}}', {
method: 'PUT',
data: {
street: $('#street').val(),
city: $('#city').val(),
state: $('#state').val(),
zip: $('#zip').val()
},
complete: function() {
cancel()
location.reload()
}
})
}
這是取API請求:
fetch('/{{user.username}}', {
method: 'PUT',
headers: {
'Content-Type': 'application.json'
},
body: JSON.stringify({
street: document.getElementById("street").value,
city: document.getElementById("city").value,
state: document.getElementById("state").value,
zip: document.getElementById("zip").value
})
}).then(() => {
cancel()
location.reload()
})
}
當我它console.log
與節點的終端我得到一個空數組。
我想通過以下處理它在快遞:
app.put('/:username', function (req, res) {
console.log(req.body)
console.log("hello")
var username = req.params.username
var user = getUser(username)
user.location = req.body
saveUser(username, user)
res.end()
})