Fix Uncaught syntax error unexpected token < in electron

泄露秘密 提交于 2019-12-11 15:58:10

问题


I'm testing an electron app that will use php for some business logics. If I run the app from my MAMP stack all will work correctly, but if I run the app using electron, I will recieve an uncaught syntax error unexpected token <that is referenced to the <!DOCTYPE html> of my templates file. I read about this problem here on SO that is related to the path of my js and css files, but I can't figure out how to fix it, this because the paths are correct. I have an header.php file that include all the <head> code and it's required by all the templates files, is the problem caused by this?

header.php file:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="assets/css/fontawesome-all.min.css" type="text/css">

<script type="text/javascript" src="assets/js/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.bundle.min.js"></script>
</head>
<body>

dashboard.php template file:

<?php
require_once 'header.php';
?>
<div class="container">
  <div class="row justify-content-center">

    <div class="col-lg-4">
      <div class="card">
        <div class="card-img-top text-center">
          <i class="fas fa-plus fa-2x"></i>
        </div>
        <a class="btn btn-link" href="add"><h4 class="text-uppercase">add info</h4></a>
      </div>
    </div>

    <div class="col-lg-4">
      <div class="card">
        <div class="card-img-top text-center">
          <i class="fas fa-search fa-2x"></i>
        </div>
      <a class="btn btn-link" href="search"><h4 class="text-uppercase">search info</h4></a>
      </div>
    </div>

  </div>
</div>

</body>
</html>


回答1:


Your header.php file is a HTML document. Since you require it, it will be interpreted as PHP which causes the syntax error.

Either create a valid PHP header file or print the contents from the header.php file. I would not recommend the later.

A simple solution would be:

  1. Rename your header.php to header.tpl (.tpl as in template)
  2. Change you dashboard.php
<?php
print file_get_contents('header.tpl'); 
?>
...


来源:https://stackoverflow.com/questions/55429954/fix-uncaught-syntax-error-unexpected-token-in-electron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!