我有这个代码:
var d = [];
$('.sortable li').each( function() {
var book_id = $(this).attr('book-id');
var order = $(this).index() + 1;
d.push({'book_id': book_id, 'order': order});
});
console.log(d);
$.post('/books/reorder', d, function(resp) {
alert(resp);
});
Run Code Online (Sandbox Code Playgroud)
当我做的console.log(d);每件事看起来都很棒但是当我把它发布到我的服务器时,请求表单数据是空的.
这里有什么我想念的吗?
谢谢!
该文件指出,数据只需要PlainObject或字符串
Type: PlainObject or String A plain object or string that is sent to the server with the request.
您正在尝试发送数组.如果将其作为对象,则可以将其传递
$.post('/books/reorder', {data:d},function(resp) {
Run Code Online (Sandbox Code Playgroud)
如果您在data属性上阅读jquery.ajax文档
data Type: PlainObject or String Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting
重要的是 Object must be Key/Value pairs.