Uncaught (in promise) SyntaxError: Unexpected end of JSON input

Posted on

getting this error when using fetch to connect to my rails api

fetch(`/products/${dataid}/description`)
.then(res=>res.json())
.then(json=>console.log(json))

 

in the browser I was able to get to the page

http://127.0.0.1:3000/products/11/description

 

answer

 

rails action was rendering basic text not json, so I had to change fetch

***Rails

def description
product=Product.find(params[:id])
if product
if product.description
render plain: product.description
else
render plain: “No description”
end end end

***JS

fetch(`/products/${dataid}/description`)
.then(res=>res.text()) //<————————————-
.then(json=>console.log(json))

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s