如何使用AJAX从.php文件中加载内容? [英] How do i load content from .php files with AJAX?

查看:77
本文介绍了如何使用AJAX从.php文件中加载内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AJAX和History.js将内容从单独的页面加载到索引页面.但是由于某些原因,我只能加载.html页-如何使此函数加载.php页?

Im using AJAX and History.js to load content from separate pages into index-page. But for some reason i can only load .html-pages - how do i make this function load .php-pages?

在var = urlPath中添加扩展名的解决方案吗?还有其他想法吗?

Is the solution to add extensions in in var = urlPath? Other ideas?

// AJAX LOAD ONKLIK
$('body').on('click', 'a', function(e) {
    var urlPath = $(this).attr('href');
    var title = $(this).text();

    History.pushState({urlPath: urlPath}, title, urlPath);
    $("#content").load(urlPath);      
    return false; 
}); 

我使用没有扩展名的链接来加载页面"about.html":

I using links with no extension for example loading the page 'about.html':

<a href="/about">About</a>

在本地测试时会在浏览器中生成此网址:

which generates this url in browser when testing locally:

http://localhost/about

那没关系,但是当我尝试以相同的方式加载.php-page fx'home.php'时没有任何反应-没有加载.好像AJAX/History.js会以某种方式中断.

Thats all ok, but nothing happens when i try loading a .php-page fx 'home.php' the same way - nothing gets loaded. Seems like the AJAX / History.js breaks somehow.

但是-如果我在页面'home.php'的链接中添加扩展名.php,则该页面通过AJAX加载,就像.html-pages一样:

BUT - if i add the extension .php in the link for page 'home.php', the page loads via AJAX just like .html-pages:

<a href="/home.php">About</a>

不过,此链接将在本地测试的浏览器中添加扩展名.php:

However this link will add the extension .php in the browser on local test:

http://localhost/home.php

那么有谁知道History.js在使用AJAX时是否仅接受.html文件,还是我在这里遗漏了什么?

So does anyone know if History.js only accepts .html files when working with AJAX, or am i missing something here?

推荐答案

最后编辑/答案:

我找到了问题的答案-实际上,该问题与AJAX/History.js无关,但与本地测试服务器有关.

I found an answer to my question - actually the issue wasnt related to AJAX / History.js, but had to do with my local test-server.

为了重写URL,以便链接可以无扩展名起作用,请在.htaccess中添加以下规则,然后将其放置在服务器的根目录中-现在,如果链接看起来像这样,则将同时加载.html和.php页面<a href="/home">About</a>

In order to rewrite urls so link will work with no extension, add the following rules in an .htaccess og place it in root of server - now both .html and .php - pages will load if the link looks like this <a href="/home">About</a>

选项+关注符号链接

RewriteEngine开启

RewriteEngine On

RewriteCond%{REQUEST_FILENAME}!-d

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond%{REQUEST_FILENAME} .html -f

RewriteCond %{REQUEST_FILENAME}.html -f

RewriteRule ^(.*)$ $ 1.html

RewriteRule ^(.*)$ $1.html

RewriteCond%{REQUEST_FILENAME}!-d

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond%{REQUEST_FILENAME} .php -f

RewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.*)$ $ 1.php

RewriteRule ^(.*)$ $1.php

这篇关于如何使用AJAX从.php文件中加载内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆