Syntaxerror: Unexpected Token O in Json At Position 1

Unexpected token o in JSON at position 1, what's wrong?

You are fetching data in wrong way. This is the correct way fetching data from api using axios.

response = axios.get('https://opentdb.com/api.php?amount=1&type=multiple&encode=url3986').then((response) => {
let data = response.json()
console.log(data)
}).catch(err => console.log('error occured', err))

You need to use .json() method to parse data.

Unexpected token o in JSON at position 1 fetch request

Long story short the problem was above what i posted, i used express.json() as a middleware which was what i needed but i didn't use it properly.
Using express.json() as middleware expects the body of the request to be a string so that it can convert it into json, so in react i used JSON.stringify(jsonObject) where jsonObject was the fetch body content that i needed.

Unexpected token o in JSON at position 1 OR Cannot read property '0' of undefined

When using jQuery's $.ajax, it checks the content-type header returned by the server. If it's set to application/json, then it runs JSON.parse() for you.

This means your response may already be an object (and not a string that needs to be "parsed").

Also, your data is a single object, not an array of objects, there is no row property/array. You just need to use $("#id").val(response.id);, etc.

Unexpected token o in JSON at position 1 in console

You're trying to decode an object, not a JSON string.

The thing you're trying to decode is being serialized before it gets passed to the JSON decoder as [object Object], hence the o at index 1.

Go back to the getJSON docs and you'll see you're not setting up your callback correctly. The return value you're storing in res is not what you think it is. You need to collect an argument to your callback function -- this is where your decoded response will be.

This should get you on the right lines:

$.getJSON("menu.json")
.done(function(obj) {
console.log(obj.name);
});

How to fix "SyntaxError: Unexpected token o in JSON at position 1" in NodeJS/Express server

Your server-side error does not indicate a CORS error. That indicates a JSON parsing error.

It appears to me that you are not correctly sending JSON content so the recipient is getting a JSON parsing error. With fetch(), you have to manually turn your data into JSON before sending. Change this:

body: {
docId: $('#id').val(),
stripeId: USER_ID
},

to this:

body: JSON.stringify({
docId: $('#id').val(),
stripeId: USER_ID
}),

See JSON sending example on MDN.

SyntaxError: Unexpected token in JSON at position 0 Express

First, stop using body-parser. Express has its own request body parsing middleware.

app.use(express.json())
app.use(express.urlencoded()) // extended = true is the default

The JSON parsing middleware is configured to handle objects by default. While a string literal like "hello world" is valid JSON, it's not what the framework expects, hence your error.

Since you appear to be trying to access req.body.post, you should send your data with such a structure

fetch("http://myip:3000/wow/post", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ post: "hello world" })
})

Alternatively, if you did want to post a JSON string literal, you would need to configure your JSON middleware like so

app.use(express.json({ strict: false }))

strict

Enables or disables only accepting arrays and objects; when disabled will accept anything JSON.parse accepts.

in which case your "hello world" string would appear in req.body



Related Topics



Leave a reply



Submit