将$ 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' },
- 节点/expression4:在ajax文章上用express-validator显示错误
- 与http服务器,websocket和快递的体系结构
- Nodejs,express,Ajax:函数只触发六次
- 如何在Multipart superagent请求中发送一个Object和一个附加文件?
- 节点js bodyparser ajax调用并在html中输出
- nodejs https api请求错误错误:在TLSWrap.onread(net.js:568:26)处的exports._errnoException(util.js:1018:11)处读取ECONNRESET
- 如何validation服务器调用中的recaptcha?
- 节点js脚本与ajax的XML请求不工作
- Ajax Express NodeJS在页面的一部分加载ejs模板