0

我正在向后端推送一个 AJAX“PUT”调用。我似乎无法使用来自 AJAX 调用的数据获取“req.body”对象。

它与它有关form.serialize()吗?
有什么建议么?

路线/index.js

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

图书馆BookForm.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')

图书馆BookAjax.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);
});
4

1 回答 1

2

contentType不应该是上面所说的 。

更改它以
contentType: "application/x-www-form-urlencoded; charset=UTF-8"
产生正确的结果。

于 2013-09-24T00:24:47.177 回答