Accessing Client's 'localhost' from JavaScript Online

后端 未结 5 750
旧时难觅i
旧时难觅i 2021-01-05 09:01

this is what I am trying to do.

  1. I have made a few .html pages with JavaScript code in it and hosted them on a Yahoo server.

  2. Now when a clien

相关标签:
5条回答
  • 2021-01-05 09:20

    Due to policy restrictions browsers do not allow you to send XMLHttpRequest to domains different than the domain hosting the web page which in your case is Yahoo.

    0 讨论(0)
  • 2021-01-05 09:34

    As others have commented, this doesn't work because of the browser security model.

    You might be able to get around this with an entry in your hosts file.

    First, assuming your app is on a yahoo.com domain, open your hosts file and add an entry like this

    127.0.0.1 mylocalhost.yahoo.com
    

    Then, in your pages, change your AJAX endpoint to http://mylocalhost.yahoo.com/myservlet/serverl1

    I've never tested this, so I can't be certain it will work, but it might. If it does work, every user of this page will need to modify their hosts file like above

    Note: your hosts file should be located at C:\WINDOWS\system32\drivers\etc\hosts in windows, and at /etc/hosts in *nix

    0 讨论(0)
  • 2021-01-05 09:40

    Isn't this a cross-domain problem?

    0 讨论(0)
  • 2021-01-05 09:42

    The local machine also needs a proxy set up that maps "http://localhost:8080/whatever" to the yahoo pages with your Ajax code. In order for the code to work, you must load it in the browser using the domain same domain that it tries to access.

    I'm not sure how to do this with Tomcat (?), but one option is to use Apache to proxy both the Tomcat server and the Yahoo pages into the same location.

    In Apache, this looks like:

    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    ...
    <IfModule proxy_http_module>
      ProxyRequests off
      ProxyPass /static    http://yahoo.com/path
      ProxyPass /myservlet http://localhost:8080/myservlet
    </IfModule>
    

    You would then load your HTML from localhost/static, and those pages would be able to make AJAX requests to localhost/myservlet.

    0 讨论(0)
  • 2021-01-05 09:45

    Cross-site Scripting

    You cannot access what is not on your domain, unless it is a Web Service returning XML or JSONP

    0 讨论(0)
提交回复
热议问题