Localhost和打开的html文件之间的区别 [英] Difference between Localhost and opening html file

查看:627
本文介绍了Localhost和打开的html文件之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用localhost中的服务器运行文件和打开文件(如 file:///Users/$user_name/$your_directory/index.html 假设没有后端被使用,它只是前端并包含html / css / js

这是否也会影响与其他服务器的交互。 ajax请求?

我很抱歉,如果这太宽泛,但我还没有找到这些潜在问题的坚实答案。

从根本上讲,假设在某个时刻你要将结果托管在实际的Web服务器上,前者与目标环境匹配,而后者则不匹配。虽然浏览器非常相似,但浏览器会将从Web服务器提供的本地文件和文件(甚至是 localhost web服务器)不同。其中一个方面是编码:当您从Web服务器检索文件时,确定数据编码的过程与打开本地文件不同。


这也会如何影响与其他服务器的交互。 ajax请求?

这是处理不同的主要方式之一,甚至会因浏览器而异。从 file:// URL加载的页面从相同原点策略的角度来看,其源 null 。某些浏览器(如Chrome)完全不允许源< null 的跨源资源共享,即使您尝试与之通话的服务器有一个全开CORS政策( * )。其他(如Firefox)允许源 null 匹配通配符。



通常,为了获得最佳结果,请确保您的开发环境将以重要的方式与您的部署环境相匹配。这意味着使用Web服务器进程而不是本地文件进行开发。大多数IDE都会为你提供这个过程。如果没有,Apache或Nginx并不难安装。


What is the fundamental difference running a file using a server in localhost, and opening a file such as file:///Users/$user_name/$your_directory/index.html, assuming no backend is used, and it is only frontend and contains html/css/js
How does this also affect interactions with other server ie. ajax requests?
I am sorry if this is too broad, but I haven't found a solid answer to these underlying questions.

解决方案

Fundamentally, assuming at some point you're going to host the result on an actual web server, the former matches the target environment while the latter doesn't. Browsers treat local files and files served from web servers (even localhost web servers) differently, although very similarly. One aspect of this is the encoding: When you retrieve a file from a web server, the process of determine what encoding the data is in is different from opening a local file.

How does this also affect interactions with other server ie. ajax requests?

This is one of the primary ways in which they're handled differently, and it even varies from browser to browser. A page loaded from a file:// URL has origin null from a Same Origin Policy standpoint. Some browsers (like Chrome) disallow Cross-Origin Resource Sharing entirely for origin null, even when the server you're trying to talk to has a wide-open CORS policy (*). Others (like Firefox) allow origin null to match the wildcard.

In general, for best results, ensure that your development environment matches your deployment environment in the important ways. That means doing your development using a web server process rather than local files. Most IDEs will happily provide that process for you; if not, Apache or Nginx aren't hard to install.

这篇关于Localhost和打开的html文件之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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