简体   繁体   中英

Unexpected end of input on parameter.json()

I'm learning about servers and APIs , I do not know too much about how data is received and sended through requests. So i got my server with Node.js using Mongoose and Express.js :

server.get('/api/items', async (req, res)=>{
    let results = await TvModel.find({model: 4013});
    res.json(results);
});

So if i send the get request via postman i get this:

[
    {
        "_id": "60f62a33a1dbf8031088cd3b",
        "name": "Sony",
        "model": "4013",
        "size": 8,
        "idSerial": 1,
        "__v": 0
    }
]

So, trying to get the data via JavaScript using fetch api:

fetch(API_URL, {mode: 'no-cors'})
    .then((resolve) => {resolve.json()})

When executing this code, i get an error on the console:

Uncaught (in promise) SyntaxError: Unexpected end of input

What is the SyntaxError i cannot see?

I solved it myself by doing the following:

Thanks to @Bravo 's answer, I was able to get an idea of what my error was:

"did you add that header due to a CORS error perhaps thinking this will bypass CORS? "

Since I am using express.js for my server, I had to install a middleware for my server, an npm module called "cors" .

So the only thing i needed to add was this line of code:

const cors = require('cors');

app = express();
app.use(cors());

Anyway you can read more of this on official documentation of cors in the official express.js website

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM