Why my javascript code is not executing and says n

2020-08-24 17:14发布

问题:

Here I have included the javascripts but I got mime type is not valid :

     <script type="text/javascript" src="{{ asset('/dashboard/js/components.js') }}"></script>
      <script type="text/javascript" src="{{ asset('/dashboard/js/custom.js') }}"></script>

      <script type="text/javascript" src="{{ asset('/dashboard/vendors/slimscroll/js/jquery.slimscroll.min.js') }}"></script>
      <script type="text/javascript" src="{{ asset('/dashboard/vendors/raphael/js/raphael-min.js') }}"></script>
      <script type="text/javascript" src="{{ asset('/dashboard/vendors/d3/js/d3.min.js') }}"></script>
      <script type="text/javascript" src="{{ asset('/dashboard/vendors/c3/js/c3.min.js') }}"></script>

And I am getting the error below :

      The script from “http://localhost/Project/public/dashboard/vendors/flotchart/js/jquery.flot.pie.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.[Learn More] home
      Loading failed for the <script> with source “http://localhost/Project/public/dashboard/vendors/flotchart/js/jquery.flot.pie.js”. home:2091:1
      The script from “http://localhost/Project/public/dashboard/vendors/flot.tooltip/js/jquery.flot.tooltip.min.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.[Learn More]

What is the problem ? Please help.

回答1:

What worked for me: After a half day research i tried just to edit the JS file. Just put a new line (ENTER) after the first (commented) line and saved it.

It was jquery minified file with a comment on the beginning. In Firefox "Inspector / Network" the .js was displayed as 404 and MIME (text/html). The strange thing was that the other .js files in the same directory were OK. Hope that helps.



回答2:

I faced the same problem for Node js application. Your scripts path starting with

/dashboard/js/components.js

so basically you have to use

const app = express()
const publicDirectoryPath = path.join(__dirname, '../public/')
app.use(express.static(publicDirectoryPath))