0

I'm pushing an AJAX "PUT" call to the backend. I can't seem to get the 'req.body' object with the data from the AJAX call.

Does it have something to do with the form.serialize()?
Any suggestions?

routes/index.js

app.put('/:library/:book/:genre', function(req, res) {
  console.log(req.body.book-name);
  res.send(200, {"youKnow":"putter"});  
});

libraryBookForm.jade

form#create-library-form(action='#', method='post')
      input(name="_method", value="PUT", type="hidden")
      div
        label Book
        input#book-name(type='text', name='book-name', required='required')
      div
        label Gender
        select#book-genre(name='book-genre')
          option(value='scifi') SciFi
          option(value='fantasy') Fantasy
      div
        input(type='submit', id='create-book-submit', value='Create Book')

libraryBookAjax.js

event.preventDefault();
$.ajax({
  url: '/publicLibrary/drawingBook/fantasy'
  type: 'PUT',
  contentType: 'application/json: charset=utf-8',
  dataType: 'json',
  data: form.serialize()
}).done(function(msg) {
  alert("put success: " + msg);
}).fail(function(msg) {
  console.log("failure: " + msg);
});

1 Answer 1

2

The contentType should not be what is stated above.

Changing it to
contentType: "application/x-www-form-urlencoded; charset=UTF-8"
produces the correct result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.