从ajax获取数据后重新加载页面

时间:2011-11-29 07:17:39

标签: php jquery ajax

我正在使用ajax动态获取数据,但是存在问题。刷新页面时,获取的数据将丢失。我希望刷新页面后数据保持不变。

假设这是我的页面:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
/* fetching data which matches the keyword*/
function Search()
    {
    $.post("search.php", {search_word: $('#searchInput').val()} , function(data){ 
    if(data){
        $('#results').show();                   
        $("#results").append(data);                 
    }
    });
    }
</script>
</head>

<body>
<input type="text" value="" id="searchInput"/>
<input type="button" value="Search" onclick="Search();"/>
<div id="results" style="display:none"></div>
</body>
</html>

当我点击搜索按钮时,刷新页面后,结果div中的数据将会消失。

2 个答案:

答案 0 :(得分:1)

将该数据保存在sessiondatabase中,然后从那里获取 因为刷新页面getpost变量后会自动销毁。

答案 1 :(得分:1)

考虑使用cookie数据。

这个jQuery插件可以解决这个问题: http://plugins.jquery.com/project/cookie

$.cookie('example', 'foo'); //the name is example and that value is foo.

获取cookie的价值很简单......

alert($.cookie('example');

因此,在动态加载数据后,您可以将该数据存储在cookie中。虽然如果您获取大量数据,这可能是一个糟糕的解决方案。

由于缺乏示例代码而无法理解您的意图或您正在做什么/您正在做什么,这个问题很难回答。

相关问题