I have a simple API with symfony, run on http://127.0.0.1:8000 and a React project who run on http://localhost:3000.
I want to get this :
{"0":{"id":51,"nom":"Ouais","prenom":"ssdds","competences":{}},"1":{"id":52,"nom":"Ouais","prenom":"ssdds","competences":{}},"2":{"id":53,"nom":"Alexis","prenom":"un truc","competences":{}}}
URL = http://127.0.0.1:8000/api/collaborateurs
So, in react i do :
import React, { Component } from "react";
import './App.css';
fetch('/api/collaborateurs')
.then(response => console.log(response))
.then(json => console.log(json))
class App extends Component {
render() {
return (<div className="App">
<h1>Salope</h1>
</div>)
}
}
export default App;
but
console.log(response)
return :
Response {type: "basic", url: "http://localhost:3000/api/collaborateurs", redirected: false, status: 200, ok: true, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:3000/api/collaborateurs"
__proto__: Response
why url: "http://localhost:3000/api/collaborateurs" i want url: "**http://127.0.0.1:8000**/api/collaborateurs"
if i change fetch('/api/collaborateurs') to fetch('http://127.0.0.1/api/collaborateurs') the console.log not working
Thank you
http://localhost:8000/api/collaborateurs?fetch('/api/collaborateurs')will fetch from the current host which is the react app at localhost:3000. 127.0.0.1/api/collaborateurs doesn't include any port number.fetchfunction has to be converted either to text or json. Tryfetch('/api/collaborateurs').then( d => d.json() ).then(response => console.log(response))