gpt4 book ai didi

jQuery $.post 表单数据未定义

转载 作者:行者123 更新时间:2023-12-01 06:48:07 26 4
gpt4 key购买 nike

我有这个代码:

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

当我执行 console.log(d); 时,一切看起来都很棒,但是当我将其发布到服务器时,请求表单数据为空。

我在这里缺少什么吗?

谢谢!

最佳答案

docs声明数据只接受 PlainObject 或 String

Type: PlainObject or StringA plain object or string that is sent to the server with the request.

You are trying to send over an array. You can pass it over if you make it an object

$.post('/books/reorder', {data:d},function(resp) {

如果您阅读jquery.ajax有关数据属性的文档

dataType: PlainObject or StringData 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 automaticprocessing. 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

重要的部分是对象必须是键/值对。

关于jQuery $.post 表单数据未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20083417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com