【发布时间】:2020-07-30 09:26:18
【问题描述】:
我想将值从 js 传递到存储在本地存储中的 php
js
let cartItems = localStorage.getItem("productsInCart");
var jsonString = JSON.stringify(cartItems);
$.ajax({
url:"read.php",
method: "post",
data: {data : jsonString},
success: function(res){
console.log(res);
}
})
php
<?php
print_r($_POST)
?>
在 .php 上,我得到的只是“array ()”
【问题讨论】:
-
localStorage 总是返回一个字符串,你不需要
JSON.stringify它 -
但我仍然只得到空白数组()
-
你确定你在localStorage中有东西吗?你在 ajax 之前检查过 console.log(cartItems) 吗?它显示了什么?
-
是的,console.log(cartitems) 完美运行
-
我刚刚创建了一个简单的example,它可以工作,但不确定你的有什么不同(F9 启动它)。我唯一能想到的是你的
localStorage是空的
标签: javascript php html ajax cart