日期:2014-05-19  浏览次数:20668 次

localhost跟127.0.0.1小问题
最近遇到一个小问题,就是我用localhost当成网址打开页面做测试,但是我的网页js代码里面用了127.0.0.1做一个servlet请求,这时候神奇发现收不到response。我就结了很久,后来把ja代码中的127.0.0.1改成localhost才算成功了。我知道在做请求时候连ip都不用打,浏览器会帮你补全,但是为什么会出现这种bug呢,有点想不通。。大神们解疑下。

------解决方案--------------------
看看 hosts 文件
------解决方案--------------------
查看
C:\WINDOWS\system32\drivers\etc\hosts

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

127.0.0.1 localhost

------解决方案--------------------
探讨
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
这里不是说了DNS会自己识别,为什么还要自己写上。。

引用:

看看 hosts 文件

------解决方案--------------------
客户端会先去查找hosts这个文件,然后才去找DNS