[英]How to change local address in fetch API React
I have a local API which is hosted in http://127.0.0.1:8080
and React server is in http://127.0.0.1:5500
.我有一个本地 API 托管在
http://127.0.0.1:8080
和 React 服务器在http://127.0.0.1:5500
.. I want to design a user interface for the API by a React webpage.我想通过 React 网页为 API 设计一个用户界面。 But React puts its local address in front of the Get Api like
http://127.0.0.1:5500/public/127.0.0.1:8080/lst/users
.但是 React 将其本地地址放在 Get Api 之前,例如
http://127.0.0.1:5500/public/127.0.0.1:8080/lst/users
。 Even if I just put /lst/users
in the fetch method address and define a proxy "proxy": "http://127.0.0.1:8080"
in the app.json, the React will try to get http://127.0.0.1:5500/lst/users
.即使我只是把
/lst/users
放在 fetch 方法地址中,并在 app.json 中定义一个代理"proxy": "http://127.0.0.1:8080"
,React 也会尝试获取http://127.0.0.1:5500/lst/users
。 How should I fix this.我该如何解决这个问题。
// get all entities - GET
fetch('127.0.0.1:8080/lst/users', {
"method": "GET",
mode:'no-cors',
url:'http://127.0.0.1:8080',
credentials:'include'
})
.then(response => response.json())
.then(response => {
this.setState({
friends: response
})
})
.catch(err => { console.log(err);
});
You need to add the protocol else it will be treated as relative path.您需要添加协议,否则它将被视为相对路径。
add "http" => http://127.0.0.1:8080
添加“http”=>
http://127.0.0.1:8080
Eg例如
fetch('http://127.0.0.1:8080/lst/users', {
"method": "GET",
mode:'no-cors',
credentials:'include'
})
.then(response => response.json())
.then(response => {
this.setState({
friends: response
})
})
.catch(err => { console.log(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.