javascript - Converting $ajax to fetch() for PUT request -


i have tried translate following jquery code use fetch api instead. makes put request:

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

this fetch api request:

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

when console.log terminal node empty array.

i trying process in express following:

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

i assume you're using bodyparser in express because otherwise jquery version not work.

headers: {     'content-type': 'application.json' }, 

should be

headers: {     'content-type': 'application/json' }, 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -