mattymue
mattymue

Reputation: 15

Javascript: can access url that returns json but when fetching, an error occurs

When I access the URL directly, I see the JSON but I am getting an error when using 'fetch' to get the data. The error states that uncaught v in promise.

fetch('http://openlibrary.org/api/books?bibkeys=ISBN:157322359X&jscmd=data')
.then((resp) => {
    return resp.json()
})
.then((data) => {
    console.log(data);
})

Upvotes: 0

Views: 47

Answers (2)

user9572013
user9572013

Reputation:

You are receiving this error because at the JSON endpoint you don't have pure JSON, you have JavaScript. That's why it's complaining about a 'v', because it has to be JSON.

You should just remove the var ... = on the endpoint

Upvotes: 0

Satvik Daga
Satvik Daga

Reputation: 156

Change your code to the following and it will work. I have just added a paramater key format with json value.

fetch('http://openlibrary.org/api/books?bibkeys=ISBN:157322359X&jscmd=data&format=json')
.then((resp) => {
    return resp.json()
})
.then((data) => {
    console.log(data);
})

Upvotes: 1

Related Questions