我已经将表单数据存储在本地存储数组中,我希望使用ajax将该数组发送到php页面,并希望访问php页面中的数据。
这是我的密码-
var myvalue=document.getElementById('name').value;
var favorites_str = localStorage.getItem('my_favorites');
if(favorites_str == null) {
favorites = [];
favorites.push({ "name":myvalue});
}
else{
favorites = JSON.parse(favorites_str);
favorites.push({ "name":myvalue});
}
localStorage.setItem('my_favorites',JSON.stringify(favorites));
var data = localStorage.getItem('my_favorites');
if(data == null){
alert("0 favorites");
}else{
favorites = JSON.parse(data);
$.each(favorites, function(index,item){
var my_items=item.name;
})
};
if(navigator.onLine)
{
$.ajax({
url:'http://localhost/offline/nextpage.php',
type:'post',
data:{my_items:my_items},
success:function(data)
{
$('#result').html(data);
}
});
}谢谢。
发布于 2016-10-24 11:55:14
假设您用键名"name“将数据存储在本地存储中,您可以像下面这样获得数据,并将其发送到ajax调用中。
data: {name: localStorage.getItem('name')}发布于 2016-10-24 12:15:01
我在代码中看到的问题是,您试图在ajax请求时发送一个变量(my_items),但是变量定义在不同的范围内。
试一试:
var myvalue=document.getElementById('name').value;
...
favorites = JSON.parse(data);
var my_items = [];
$.each(favorites, function(index,item){
my_items.push(item.name);
});
if(navigator.onLine) {
$.ajax({
url:'http://localhost/offline/nextpage.php',
type:'post',
data:{my_items:my_items},
success:function(data)
{
$('#result').html(data);
}
});
}现在,my_items对象将可以在ajax请求中访问。
https://stackoverflow.com/questions/40217579
复制相似问题