Javascript fetch和pushState是否存在特定行为?
我在尝试使用pushState获取一些数据时遇到问题 所以我基本上有4个功能:Javascript fetch和pushState是否存在特定行为?,javascript,ajax,fetch,pushstate,html5-history,Javascript,Ajax,Fetch,Pushstate,Html5 History,我在尝试使用pushState获取一些数据时遇到问题 所以我基本上有4个功能: 单击事件侦听器 获取所需链接的函数 触发动画的函数 以及获取此url的函数 在我试图推动历史上的一个国家之前,一切都很顺利 在我的获取请求之前或之后,history.pushState会出于某种原因中断请求,如:controller/controller/about.php 我尝试了一个console.log(url);在所有4个函数上,结果都是相同的(例如,获取一个好的url:controller/about.
- 单击事件侦听器
- 获取所需链接的函数
- 触发动画的函数
- 以及获取此url的函数
const loadNewContent = async url => {
const response = await fetch(url, { headers: headers });
// Tried adding pushState inside fetch request
// window.history.pushState(url, `${url}`, url);
};
// function that fires animations then load the last function
const changePage = url => {
loadNewContent(url).catch(error => console.error(error));;
};
const ajax = e => {
// Tried adding pushState before fetch fires
// window.history.pushState(newPage, ``, newPage);
changePage(newPage);
// Tried adding pushState after fetch fires
// window.history.pushState(newPage, ``, newPage);
};
// Click event fires ajax function
ajax(e);
这就是相对路径的工作原理。最后一个
/
之后的所有内容都将被丢弃,并追加相对URL
http://localhost/imparfait
+controller/about.php
变成http://localhost/controller/about.php
当
http://localhost/imparfait/controller/article.php
+controller/about.php
变成http://localhost/controller/controller/about.php
使用绝对路径(以
/
开头)、方案相对URL(以//localhost
开头)、绝对URL(以http://
开头),或者构造一个URL,使用该URL可以指定基
,而不是获取
从位置获取。href
调用pushState
前后页面的URL是什么?如果我在changePage(newPage)之前调用pushState;url是controller/article.php,但fetch返回一个404,试图在changePage(newPage)之后访问controller/controller/article.phpIf,我调用pushState;同样的行为,我认为你误解了。在调用pushState
之前和之后,浏览器地址栏中的URL是什么?之前:root(localhost/folder)之后:localhost/folder/controller/article.phpOh,就这么简单!谢谢你的帮助和耐心@Quentin