javascript - Fetch in JS自动添加当前页面url,如何去掉

标签 javascript fetch

所以我正在尝试发出这样的获取请求:

var url = "";
const domain = window.location.host;

if (sort === "all")  {
    url = domain + "/api/post-list/all";
}
else {
    url = domain + "/api/post-list/" + sort;
}

fetch(url)
.then(response => response.json())
.then(posts => {
  
  console.log(posts);
  posts.forEach(post => update_posts(post, sort));

}); 

问题是,fetch 会自动添加当前页面的 url,但我不需要它,所以它最终会这样请求:

http://127.0.0.1:8000/profile/127.0.0.1:8000/api/post-list/enn

当它应该只是这个 http://127.0.0.1:8000/api/post-list/enn 时。

或者从这个http://127.0.0.1:8000/127.0.0.1:8000/api/post-list/all到这个http://127.0.0.1:8000/api/post-list/all

有办法吗?

最佳答案

试试这个

const domain = window.location.origin

关于javascript - Fetch in JS自动添加当前页面url,如何去掉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63424694/

相关文章:

javascript - 忽略提取中的自签名 SSL 证书

react-native - 如何通过GET请求通过获取API发送数据?

javascript - document.getElementById ('foo' ).src ='' 不起作用

javascript - Node JS/Gzip : Image file download ends prematurely with no error

javascript - JQuery 移动网站中的 CSS 和 JS 文件

javascript - 将三元开关添加到按钮 - Javascript

javascript - 如何在AngularJS中切换两个元素的显示

php - 如何从 mysql 获取类别到 ul 中?

javascript - React Native 渲染 FlatList 有条件

javascript - 正在获取 url