我一直绞尽脑汁,几乎整整一天都在搜索谷歌,为什么这样做不起作用.作为改进我自己网站的实验,我在这里查看教程http://tutorialzine.com/2009/09/simple-ajax-website-jquery/最重要的是ajax/jquery部分.
基本上,该教程的POST与页码一起使用,但我一直在尝试将其转换为使用页面名称.因此,从href开始需要#!home(hashbangs用于实现谷歌兼容性),php可以将其解析为"home.html"并将其加载到内容div中.由于我以外的原因,它不会起作用.我将发布我试图修改的相关代码段,对我有利:
从javascript加载器(只有我修改的结束部分):
var datastring=url.replace('#!',''); //strip the #page part of the hash and leave only the page number
$('#loading').css('visibility','visible'); //show the rotating gif animation
$.ajax({ //create an ajax request to load_page.php
type: "POST",
url: "load_file.php",
data: datastring, //with the page number as a parameter
dataType: "html", //expect html to be returned
async: false,
success: function(msg){
if(parseInt(msg)!=0) //if no errors
{
$('#content').html(msg); //load the returned html into pageContet
$('#loading').css('visibility','hidden'); //and hide the rotating gif
}
}
});
Run Code Online (Sandbox Code Playgroud)
和整个php文件:
<?php
$url = $_POST['datastring'];
if(file_exists(''.$url.'.html'))
echo file_get_contents(''.$url.'.html');
else echo 'There is no such page!';
?>
Run Code Online (Sandbox Code Playgroud)
我想自己学习并想出来,但说实话,我不明白:/据我所知,没有跨域问题.谁知道我错过了什么?我想在这里问一下,因为它可能比该教程的网站更多,但是如果我找到一个解决方案,我会去那里并将其发布在评论中,以便其他人可以避免我的痛苦.XD
您需要将POST数据作为键值对传递.
尝试将您的数据更改为:
data: "datastring="+datastring
Run Code Online (Sandbox Code Playgroud)