Below is my attempt to convert the following jquery's AJAX call using the fetch
function:
const sendingData = {...}
$.ajax({
url: "/api/data/getuser",
type: "GET",
data: sendingData ,
dataType: 'json',
ContentType: 'application/json',
success: (data) => {
console.log('SUCCESS')
console.log(data)
this.setState({
isFetching: false,
data: data.user
})
},
error: (err) => {
console.log(err)
this.setState({isFetching: false})
}
})
I'm trying to rewrite it with the fetch
method. I attempted this code snippet:
fetch("/api/data/getuser", {
method: "GET",
data: data,
dataType: 'json',
ContentType: 'application/json'
}).then((resp) => {
console.log(resp)
}).catch((err) => {
console.log(err)
})
Although the server should return an object containing user information, instead, the response object received is as follows:
Response {type: "basic", url: "http://localhost:3001/api/data/getuser", redirected: false, status: 200, ok: true, …}
body: ReadableStream
locked: (...)
__proto__: Object
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:3001/api/data/getuser"
__proto__: Response
}