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

解决方案 »

  1.   

    我用127.0.0.1当成网址打开页面做测试,但是我的网页js代码里面用了localhost做一个servlet请求,这时候也是发现收不到response。。就是说访问网址请求的要始终保持一致,不然就收不到response、、
      

  2.   

    查看
    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 host127.0.0.1       localhost
      

  3.   


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

  4.   

    但是你没有DNS啊
    你的127.0.0.1和你的localhost不是一个东西啊
      

  5.   

    客户端会先去查找hosts这个文件,然后才去找DNS
      

  6.   

    看看 hosts 文件
    最后一行
    127.0.0.1 localhost
      

  7.   


    你的DNS解析的lcalhost肯定不是你的机器(就算DNS默认配置了localhost也是DNS自己),你想,每台机器都叫localhost,你的DNS能智能地解析成哪台机器(除非DNS配置了解析到某台机器,否则DNS也解析不了)?
    所以,这种情况,修改你的hosts文件,打开
    127.0.0,1 localhost
    就可以了