gpt4 book ai didi

javascript - javascript(particlesJS)的不一致行为

转载 作者:IT王子 更新时间:2023-10-29 02:01:58 27 4
gpt4 key购买 nike

我正在尝试制作一个登录页面。下面是主 html 页面的正文(为了简洁起见,只显示正文):

<body>
<div class="container">
<div id="login-box">
<div class="controls">
<input type="text" name="username" placeholder="Username" class="form-control" />
<input type="text" name="username" placeholder="Password" class="form-control" />
<button type="button" class="btn btn-default btn-block btn-custom">Login</button>
</div> <!-- /.controls -->
</div> <!-- /#login-box -->
</div> <!-- /.container -->

<div id="particles-js"></div>
<script src="https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js"> </script>

<script>
particlesJS.load('particles-js', 'particles.json', function() {
console.log('callback - particles.js config loaded');
});
</script>

</body>

除此之外,我还有一个 css 和 particles.json 静态文件。

当我在使用 apache2 作为 Web 服务器的 Web 浏览器上执行它时,一切正常。

然后我获取完全相同的文件并在 Golang 程序中使用它:

func main(){
templates := template.Must(template.ParseFiles("../../templates/index.html"))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request){
if err:= templates.ExecuteTemplate(w, "index.html", nil) ; err != nil{
http.Error(w, err.Error(), http.StatusInternalServerError)
}
//fmt.Fprintf(w, "Welcome to Gopherland")
})
http.Handle("/css/", http.FileServer(http.Dir("style")))
http.Handle("/js/", http.FileServer(http.Dir("style")))
http.ListenAndServe(":5000", nil)
}

它无法解析 particles.json 文件:

语法错误:JSON.parse:JSON 数据第 1 行第 1 列出现意外字符

Go 不会抛出其他编译时或运行时错误。此错误出现在浏览器控制台中。我已经检查了 particles.json 文件的有效性。

有人可以帮忙吗?

particles.json 的内容:

{
"particles": {
"number": {
"value": 80,
"density": {
"enable": true,
"value_area": 800
}
},
"color": {
"value": "#ffffff"
},
"shape": {
"type": "circle",
"stroke": {
"width": 0,
"color": "#000000"
},
"polygon": {
"nb_sides": 5
},
"image": {
"width": 100,
"height": 100
}
},
"opacity": {
"value": 0.5,
"random": false,
"anim": {
"enable": false,
"speed": 1,
"opacity_min": 0.1,
"sync": false
}
},
"size": {
"value": 5,
"random": true,
"anim": {
"enable": false,
"speed": 40,
"size_min": 0.1,
"sync": false
}
},
"line_linked": {
"enable": true,
"distance": 150,
"color": "#ffffff",
"opacity": 0.4,
"width": 1
},
"move": {
"enable": true,
"speed": 6,
"direction": "none",
"random": false,
"straight": false,
"out_mode": "out",
"attract": {
"enable": false,
"rotateX": 600,
"rotateY": 1200
}
}
},
"interactivity": {
"detect_on": "canvas",
"events": {
"onhover": {
"enable": true,
"mode": "repulse"
},
"onclick": {
"enable": true,
"mode": "push"
},
"resize": true
},
"modes": {
"grab": {
"distance": 400,
"line_linked": {
"opacity": 1
}
},
"bubble": {
"distance": 400,
"size": 40,
"duration": 2,
"opacity": 8,
"speed": 3
},
"repulse": {
"distance": 200
},
"push": {
"particles_nb": 4
},
"remove": {
"particles_nb": 2
}
}
},
"retina_detect": true,
"config_demo": {
"hide_card": false,
"background_color": "#b61924",
"background_image": "",
"background_position": "50% 50%",
"background_repeat": "no-repeat",
"background_size": "cover"
}
}

最佳答案

http.HandleFunc("/", 匹配任何在其他地方不匹配的东西,它本质上是一个包罗万象的东西,如 ServeMux 文档所述:

Patterns name fixed, rooted paths, like "/favicon.ico", or rooted subtrees, like "/images/" (note the trailing slash). Longer patterns take precedence over shorter ones, so that if there are handlers registered for both "/images/" and "/images/thumbnails/", the latter handler will be called for paths beginning "/images/thumbnails/" and the former will receive requests for any other paths in the "/images/" subtree.

由于它正在尝试加载 particles.json,并且没有更好的匹配项,因此它正在加载您的索引页面。

您需要添加另一条路线,例如:

http.HandleFunc("/particles.json", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./path/to/particles.json")
})

关于javascript - javascript(particlesJS)的不一致行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50338707/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com