为什么我使用 localhost 无法得到结果?
我的电脑ip是192.168.11.3,我进行编码,并向localhost发送getJSON请求,getJSON路径是这样的:
http://localhost:8080/requestJSON.php
当我转到路径时它工作正常:
http://localhost:8080/myApplication.php
但是当我将其更改为 192.168.11.3 时,如下所示:
http://192.168.11.3:8080/myApplication.php
我使用相同的代码, getJSON 仍然请求
http://localhost:8080/requestJSON.php
中的 JSON但我不能得到我想要的。我在同一台计算机上使用这两个链接,当我访问localhost和192.168.11.3时,它应该是相同的。会发生什么?谢谢。
My computer ip is 192.168.11.3, and I do my coding, and I send getJSON request to localhost, the getJSON path is something like this:
http://localhost:8080/requestJSON.php
it works fine when I go to the path:
http://localhost:8080/myApplication.php
But when I changed it to 192.168.11.3, like this :
http://192.168.11.3:8080/myApplication.php
I use the same code, the getJSON still request the JSON in
http://localhost:8080/requestJSON.php
But I can't get what I want. I use this two links in my same computer, it should be the same, when I access localhost, and 192.168.11.3. What happens? thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为一种可能性是您的浏览器在尝试解析地址之前强制执行同源策略。尝试更改
requestJSON.php
URL 以匹配您请求的 URL 地址,并查看是否有效。I suppose one possibility is that your browser is enforcing the same origin policy before trying to resolve the address. Try changing the
requestJSON.php
URL to match the address of the URL you're requesting and see if that works.您可能需要从与请求该 JSON 的页面相同的主机请求 JSON。因此,例如
http://192.168.11.3:8080/myApplication.php
需要从http://192.168.11.3:8080/requestJSON.php
请求它。最简单的方法是在请求中使用相对 URL。You probably need to request the JSON from the same host as the page from which you're requesting it. So for instance
http://192.168.11.3:8080/myApplication.php
needs to request it fromhttp://192.168.11.3:8080/requestJSON.php
. The easiest way to do this is to use a relative URL in the request.