除了扩展名以外,.json,.txt,.html,.css和.js文件之间是否有区别? [英] Is there any difference between a .json, .txt, .html, .css, and .js file other than the extension?

查看:152
本文介绍了除了扩展名以外,.json,.txt,.html,.css和.js文件之间是否有区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

推动力是:我有一些JSON,已经将其保存为.txt文件了一段时间。为了更准确,我想将它们全部重命名为.json。我可以肯定它们都是UTF-8,大多数文件都可以通过Ajax发送。

The impetus is: I have some JSON that I've been saving as .txt files for a bit. I'd like to rename them all to .json to be more accurate. I'm pretty sure they are both UTF-8, as are most files that can be sent over Ajax.

无论如何,这个问题提出了一个更大的问题。

Anyways, this question raised a bigger question.

除了扩展名以外,所有这些文本类型之间是否存在真正的区别? .html,.json,.css,.js,.txt,.py等...它们在内部看起来都是相同的:一个UTF-8字符数组。

Is there any real difference between all of these text types, other than the extension? .html, .json, .css, .js, .txt, .py, etc... they all appear to be the same thing on the inside: a UTF-8 character array.

如果将它们交替使用,会不会比造成混淆更糟?这些中是否有标头?保存时它们中的任何一个编码是否不同?大多数.txt文件的扩展名都不同吗?

If you used these interchangeably, would it do anything worse than being confusing? Do any of these have headers? Do any of them encode differently when being saved? Are most .txt files with a different extension?

推荐答案

从理论上讲,您可以使用所需的后缀。后缀本身可以对文件的内容没有影响。但是应用程序会根据文件后缀执行不同的操作。因此,如果使用错误的后缀或未知的后缀,则需要告诉应用程序文件类型是什么,否则它将解释为错误。

In theory you could use what ever suffix you want.The suffix itself does not have an effect on the content of the file. But applications do different stuff depending on the file suffix. So if you use the wrong or an unknown suffix then you need to tell the application about what file type it is, otherwise it would interpret it wrong.

网络服务器可以通过默认为后缀为 htm html text / html $ c>作为 Content-Type 标头。然后客户端会将接收到的文件解释为 html ,因为该 Content-Type 。因此,如果您向JavaScript或CSS文件添加 html ,则即使加载了< link> < script>

A webserver would by default add text/html for a file with the suffix htm or html as Content-Type header. And the client then would interpret the received file as html because of that Content-Type. So if you add html to you JavaScript or CSS files then the client might not execute/use them, even if loaded with <link> or <script>.

但是如果您告诉网络服务器发送 text / css 用于 style.html application / javascript 用于 script.html ,客户端可能可以正确使用它们。网络浏览器应该使用 Content-Type 标头,然后根据后缀猜测类型。

But if you tell the webserver to send text/css for style.html and application/javascript for script.html then the client might use them correctly. A webbrowser should use the Content-Type header before guessing the type based on the suffix.

这篇关于除了扩展名以外,.json,.txt,.html,.css和.js文件之间是否有区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆