qicosmos / cinatra

modern c++(c++20), cross-platform, header-only, easy to use http framework
MIT License
1.89k stars 375 forks source link

在html中调用$(document).ready崩溃 #118

Open Archger opened 5 years ago

Archger commented 5 years ago

在html中调用$(document).ready,服务器直接崩溃。 网页body部分为:

<div class="parallax-container">
    <div class="parallax"><img src="www/images/parallax1.jpg"></div>
  </div>
  <div class="section white">
    <div class="row container">
      <h2 class="header">Parallax</h2>
      <p class="grey-text text-darken-3 lighten-3">视差</p>
    </div>
  </div>
  <div class="parallax-container">
    <div class="parallax"><img src="www/images/parallax2.jpg"></div>
  </div>

<script>
$(document).ready(function(){
      $('.parallax').parallax();
    });
  </script>

服务器代码为:

#include "cinatra.hpp"
using namespace cinatra;

int main() {
    int max_thread_num = std::thread::hardware_concurrency();
    http_server server(max_thread_num);
    server.listen("0.0.0.0", "8080");
    server.set_http_handler<GET, POST>("/", [](request& req, response& res) {
        res.render_view(./www/index.html)
    });

    server.run();
    return 0;
}

html调用了Materialize框架,在访问http://0.0.0.0:8080/时,服务器直接退出。通过验证,不使用其余任何框架,直接调用$(document).ready或者onload,服务器都会直接崩溃退出,删除js则服务器正常运行且图片正常显示。

qicosmos commented 5 years ago

因为渲染引擎里的$和jquery里的$冲突了,你需要用layui.jquery来代替js里面的$符号。可以参考这个: https://github.com/qicosmos/feather/blob/4043ec45fa1c1a691838ec5190c46d3ca614b975/purecpp/html/member_edit.html#L94