最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - strict mime type checking is enabled, refused to execute script from '<url>' its mime

programmeradmin5浏览0评论

I am newbie in Django, and i was working on a Django app. And i used "media" folder to keep my css and javascript files (i know it's not a good practice) but it was working. Some lines of code

<link href="/media/shop/bootstrap//css/bootstrap.min.css" rel="stylesheet">
<link href="/media/shop/bootstrap//css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="/media/shop/themes/css/bootstrappage.css" rel="stylesheet" />

<script src="/media/shop/themes/js/jquery-1.7.2.min.js"></script>
<script src="/media/shop/bootstrap//js/bootstrap.min.js"></script>
<script src="/media/shop/themes/js/superfish.js"></script>

However what happened i don't know after somedays(today) when i opened this project again and tried to run on local host then i am getting error in console like this
.
After clicking on the link present in error it displays my javascript file but not executing it.
And the most weired thing is that when i executed the same file in other system(also on local host) after transferring the same folder then it is working, no any such error in console. And the same project(website) is also hosted on internet and when i open that site online in my system again then also no any such error. However when i replace

<script src="/media/shop/themes/js/jquery-1.7.2.min.js"></script>

with

<script type="javascript" src="/media/shop/themes/js/jquery-1.7.2.min.js"></script>

then error not es in console but it is still not working. I don't think there is any problem with browser setting because i checked in 3 different browsers but it is showing same thing.
Any suggestion how to solve this issue and how to disable mime type checking ?

I am newbie in Django, and i was working on a Django app. And i used "media" folder to keep my css and javascript files (i know it's not a good practice) but it was working. Some lines of code

<link href="/media/shop/bootstrap//css/bootstrap.min.css" rel="stylesheet">
<link href="/media/shop/bootstrap//css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="/media/shop/themes/css/bootstrappage.css" rel="stylesheet" />

<script src="/media/shop/themes/js/jquery-1.7.2.min.js"></script>
<script src="/media/shop/bootstrap//js/bootstrap.min.js"></script>
<script src="/media/shop/themes/js/superfish.js"></script>

However what happened i don't know after somedays(today) when i opened this project again and tried to run on local host then i am getting error in console like this
.
After clicking on the link present in error it displays my javascript file but not executing it.
And the most weired thing is that when i executed the same file in other system(also on local host) after transferring the same folder then it is working, no any such error in console. And the same project(website) is also hosted on internet and when i open that site online in my system again then also no any such error. However when i replace

<script src="/media/shop/themes/js/jquery-1.7.2.min.js"></script>

with

<script type="javascript" src="/media/shop/themes/js/jquery-1.7.2.min.js"></script>

then error not es in console but it is still not working. I don't think there is any problem with browser setting because i checked in 3 different browsers but it is showing same thing.
Any suggestion how to solve this issue and how to disable mime type checking ?

Share Improve this question edited May 11, 2020 at 13:23 Abhishek Pratap Singh asked May 11, 2020 at 12:33 Abhishek Pratap SinghAbhishek Pratap Singh 7613 gold badges11 silver badges22 bronze badges 2
  • What are your django settings? You should be serving static files via the static loader and not media – Airith Commented May 11, 2020 at 17:47
  • I had this issue in the admin site after switching to another database, installing the whitenoise library helped me stackoverflow./a/72035243/7830459 – Alex Li Commented Jun 10, 2023 at 3:00
Add a ment  | 

5 Answers 5

Reset to default 3

Try adding the following code to your settings.py

import mimetypes

mimetypes.add_type("text/javascript", ".js", True)

It's a solution someone told me. Sadly, it does not e with any explanation.

in my case... it was a missing slash in the definition of STATIC_URL in settings.py:

it was like this:

STATIC_URL = "static/"

And the fix was to add a leading /:

STATIC_URL = "/static/"

The script should be type="application/javascript"

The issue lies in the configuration of the application, as it states it does required full path of the file rather than sort path. I was looking for this solution and while checking with my Wordpress site I found that it had all the static file paths having domain name like "https://domainname.tld/filepath/filename.js" or "https://domainname.tld/filepath/filename.css" similar for media files. So I tried to look for getting the host name and updating staticfile_dir to just mentioning location to static files placed location(in my case I had files in "Project_Dir/App_Dir/templates/App_Dir/Static/"). So that I updated my STATICFILES_DIR = ["Project_Dir/App_Dir/templates/App_Dir/Static/",] and when accessing static file used "<script type="text/javascript" src = "{{request.scheme}}://{{request.get_host}}{% static 'js/client.js' %}"></script>" which allowed to me for accessing the static files.

Update

To e over this error I had used a trick which allowed me to access any file from which location I wish under the same folder.

For the solution I had created another application to serve as content delivery network and further to that had create different function which reads the content of the js file and then returns it a for of URL.

By above mean if I had URL named "mydomain./foo/file/" and the relavent function to the URL is serve_file which is configured under the URLs file of the Django application.

Then under the views file serve_file function do the thing is it reads the javascript file from its specified location and then it returns the content in a form of javascript like below:

def serve_file(request):
    with open('path/to/javascript/file.js', 'r') as file:
        js_content = file.read()
    return HttpResponse(js_content, content_type='application/javascript')

Check your Windows registry key.

C:\>reg query HKCR\.js /v "Content Type"

HKEY_CLASSES_ROOT\.js
    Content Type    REG_SZ    text/plain

Update the key as following, it works for me.

C:\>reg add HKCR\.js /v "Content Type" /t REG_SZ /d application/javascript

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论