将$ ajax转换为PUT请求的提取()

我试图翻译下面的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它与terminal节点我得到一个空的数组。

我正在尝试使用Express处理它,如下所示:

 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() }) 

我假设你在Express中使用bodyParser,否则jQuery版本将无法工作。

 headers: { 'Content-Type': 'application.json' }, 

应该

 headers: { 'Content-Type': 'application/json' },