背景:
我的网页通过AJAX / jQuery从XML文件加载条目并显示它们(工作正常)
该页面还有一个表单,通过AJAX / jQuery提交到PHP文件,该文件写入XML文件,然后通过AJAX / jQuery重新加载XML条目并显示它们(工作正常)
我有以下奇怪的问题:
如果我通过FileZilla删除XML文件,或者尝试通过从本地计算机上传副本来覆盖XML文件,那么当我查看我的页面时,条目根本不会改变。实际上,如果使用新副本覆盖XML文件,我会在我的地址栏中导航到新副本,如:http://mysite.com/myxmlfile.xml,但是,在显示XML条目的页面中,旧条目(像文件从未被覆盖/删除一样仍然显示!
困惑!
这是我的PHP文件:
<?php
header('Pragma: no-cache');
header('Cache: no-cache; must-revalidate;');
function signGuestbook($entry){
$date = date("n.d.Y");
$name = $entry['name'];
$email = $entry['email'];
$comment = $entry['comment'];
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->load('guestbook.xml');
$root = $doc->firstChild;
$e = $doc->createElement('entry');
$dateNode = $doc->createElement('date', $date);
$nameNode = $doc->createElement('name', $name);
$emailNode = $doc->createElement('email', $email);
$commentNode = $doc->createElement('comments', $comment);
$e->appendChild($dateNode);
$e->appendChild($nameNode);
$e->appendChild($emailNode);
$e->appendChild($commentNode);
$root->appendChild($e);
$doc->formatOutput = true;
$doc->save('guestbook.xml');
}
function loadGuestbook(){
$gBook = new DOMDocument();
$gBook->load('guestbook.xml');
$entries = $gBook->getElementsByTagName('entry');
$entries_arr = array();
foreach($entries as $entry)
{
$date = $entry->getElementsByTagName('date')->item(0)->nodeValue;
$name = $entry->getElementsByTagName('name')->item(0)->nodeValue;
$email = $entry->getElementsByTagName('email')->item(0)->nodeValue;
$comment = $entry->getElementsByTagName('comments')->item(0)->nodeValue;
$entries_arr[] = array(
'date' => $date,
'name' => $name,
'email' => $email,
'comment' => $comment
);
}
return json_encode($entries_arr);
}
$entry = json_decode(stripcslashes($_POST['entry']), true);
if($entry != null){
signGuestbook($entry);
}
header("Content-type: text/plain");
echo loadGuestbook();
?>
这是我的JS文件:
$(document).ready(function(){
$.post('guestbook.php?' + new Date().getTime(), loadGuestbook, "text");
});
function loadGuestbook(gBook){
var gBookDiv = $('div#guestbook');
gBookDiv.empty();
var entries = JSON.parse(gBook);
$.each(entries, function(i, entry){
gBookDiv.prepend(
'<div class="entry">' +
'<span class="date">' + entries[i].date + '</span>' +
'<strong class="blue">Name:</strong><span class="name">' + entries[i].name+ '</span><br />' +
'<strong class="blue">Email:</strong><span class="email">' + entries[i].email + '</span><br />' +
'<strong class="blue">Comments:</strong><p>' + entries[i].comment + '</p>' +
'</div>'
);
});
}
function signGuestbook(){
var name = $('input#name').val();
var email = $('input#email').val();
var comment = $('textarea#comments').val();
if(name == null || name == '' || email == null || email == ''){
alert('You must provide a name and email.');
}
else{
var entry = {
"name" : name,
"email" : email,
"comment" : comment
};
var entryString = JSON.stringify(entry);
$.post('guestbook.php?' + new Date().getTime(), { entry : entryString }, loadGuestbook, "text");
$('input#name').val('');
$('input#email').val('');
$('textarea#comments').val('');
}
}
答案 0 :(得分:1)
这是一个缓存问题。为了防止缓存Ajax调用,请为Ajax调用添加时间:
$.post('myphp.php?' + new Date().getTime(), { my : dataString }, myFunction, "text");
答案 1 :(得分:0)
您可能还希望在发送XML文件之前在PHP脚本中添加它:
header('Pragma: no-cache');
header('Cache: no-cache; must-revalidate;');