您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在客户端存储临时数据,然后将其发送到服务器

如何在客户端存储临时数据,然后将其发送到服务器

当然,因为它是一个表,所以拥有一个对象数组是有意义的。请注意,对象用花括号括起来,数组用括号括起来:

var myArray = [];  // Initialize empty array
var myObject = {}; // Initialize empty object

这应该可以满足您的需求:

// Initialize variables
var newEntry, table = [];

// Create a new object
newEntry = {
  id: '',
  price: '',
  description: ''
};

// Add the object to the end of the array
table.push(newEntry);

与此相同:

// Initialize array
var table = [];

// Create object and add the object to the end of the array
table.push({
  id: '22',
  price: '$222',
  description: 'Foo'
});

您现在可以访问如下属性:table [0] .id; //‘22’

在现代浏览器上,如果您希望数据在各个会话(例如cookie)之间持久存在,则可以使用sessionStorage或localStorage对象。

当您要将数据发送到服务器时,将通过网络发送表的JSON版本:

var data = JSON.stringify(table);
其他 2022/1/1 18:15:37 有516人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶