我有一个使用 AJAX 的单页网站。我通过 URL 将参数传递给 JavaScript。但是参数导致 404 错误(因为找不到页面)。我该如何解决这个问题?示例:
www.mydomain.com/firstname/james/lastname/smith/age/53/profession/engineer 导致404错误。我希望它转到 index.html
如何进行路由?
我正在使用 jQuery。这就是我修改 URL 的方式。
function writeUrlToBrowserWindow(){
var theString = '/ch/' + currentChapter + '/' +
't1/' + text1Info.code + '/' +
't2/' + text2Code + '/' +
'a1/' + audio1Info.code + '/' +
'a2/' + audio2Code ;
window.history.pushState(null, null, theString );
}
最佳答案
您必须在此处使用url 重写。
自
www.mydomain.com/firstname/james/lastname/smith/age/53/profession/engineer
会让服务器寻找像这样的索引页
www.mydomain.com/firstname/james/lastname/smith/age/53/profession/engineer/index.html
你会得到一个错误,因为这个文件和所有的父文件夹都不存在。
通过为 URL 重写配置的 .htaccess
文件,您将告诉服务器重定向到另一个带有参数的 URL,例如:
www.mydomain.com/index.html?firstname=james&lastname=smith&age=53&profession=engineer
并提供适当的内容。
我想您还需要使用 PHP 等语言编写索引页面,以便能够检索 URL 参数。
或者您也可以在 URL 中构建哈希并在 JS 中检索它:
www.mydomain.com/index.html/#/firstname/james/lastname/smith/age/53/profession/engineer
你可以开始阅读这个 here .
关于html - 导致 404 错误的 URL 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258465/