VincentGarreau / particles.js

A lightweight JavaScript library for creating particles
https://vincentgarreau.com/particles.js/
MIT License
29.14k stars 4.84k forks source link

Unexpected token < in JSON at position 0 at JSON.parse #214

Open linjinxuan opened 7 years ago

linjinxuan commented 7 years ago

it shows "Unexpected token < in JSON at position 0 at JSON.parse", but i dont konw why

madwill commented 7 years ago

That just means you are not finding the config.json object in the load example and use a router based app which returns an html page. Perhaps you use create-react app ?

rlaurente commented 7 years ago

any solution on this error ? I'm also experiencing this problem. It just randomly occur

Thanks

ivanthemeh commented 7 years ago

Anyone ever figure out a way around this?

naimkhalifa commented 7 years ago

I'm interested too if you figure it out!

naimkhalifa commented 7 years ago

Ok. it's an easy one.

Here's how I solved it on my react app App.js component:

import particlesJS from 'particles.js';
...

class App extends Component {
componentDidMount() {
    window.particlesJS.load('particles-js', '/assets/particles.json', null);
  }

...
}

where /assets/particles.json is a in the public path of your app.

madwill commented 7 years ago

Yes, basically make sure the particle.json file is actually reachable through the path you are requesting it and it will work.

The error comes from the JSON decoder which does not expect an HTML page (which is returned by your router as 404 or default page if you didn't set your particle.json file or path correctly.

Put them in your public folder, type the address in the bar to make sure it loads and you'll be good to go !

gatzjames commented 7 years ago

It is not stated in the docs but if you check the code particlesJS is a function itself

particleJS(tagId, optionsObject)

that you can use instead of the particleJS.load method which loads a config from a JSON file.

vdfor commented 6 years ago
import 'particles.js';
import { option as particleOption } from './particles';
...
componentDidMount() {
    // if ts -- (window as any).particlesJS('particles-js', particleOption, () => {});
    window.particlesJS('particles-js', particleOption, () => {});
  }

render() {
    return (
      <div id="particles-js" />
    );
}
...
1207915357 commented 4 years ago

this is useful to me

particlesJS('particles-js', particleOption)

is not the

particlesJS.load('particles-js', particleOption)