使用IP的getJSON无效

时间:2023-01-30 01:29:29

my problem is:

我的问题是:

When I call json using the following code

当我使用以下代码调用json时

var url="http://localhost:9000/json";
$.getJSON(url,
 function(data){
    alert(data['yay']);
    });

It works PERFECT, but, my localhost IP is 10.0.0.145, and when I replace localhost with the IP address, it no longer works

它的工作非常完美,但是,我的本地主机IP是10.0.0.145,当我用IP地址替换localhost时,它就不再起作用了。

var url="http://10.0.0.145:9000/json";
$.getJSON(url,
 function(data){
    alert(data['yay']);
    });

2 个解决方案

#1


6  

You are violating the same origin policy. If you control the destination site, you should either consider using a JSONP style of call or enable the cross domain option (crossDomain) on your site and in your AJAX call.

你违反了相同的原产地政策。如果您控制目标站点,您应该考虑使用JSONP样式的调用,或者在站点和AJAX调用中启用跨域选项(cross domain)。

Your browser won't make the call, by default, if you're violating same origin. You'll know that by watching your HTTP traffic using Fiddler, FireBug or Chrome Tools. You won't see your request even executed. If that's the case, then take one of the approaches above.

默认情况下,如果您违反了相同的起源,浏览器不会进行调用。通过使用Fiddler、FireBug或Chrome工具查看HTTP流量,您将了解这一点。您甚至不会看到您的请求被执行。如果是这样,那就采取上面的方法。

I hope this helps. Good luck!

我希望这可以帮助。好运!

#2


3  

The reason is simply cross-domain protection : ajax querying of json from another domain (apparently) can only be done if specific headers are given by the server.

原因是简单的跨域保护:从另一个域(显然是)对json进行ajax查询只能在服务器给出特定的标头的情况下完成。

I suggest you read this complete reference as this is now an essential thing to know in order to code ajax based applications.

我建议您阅读完整的引用,因为要编写基于ajax的应用程序代码,现在必须了解这一点。

Use the same name for both.

两者使用相同的名称。

#1


6  

You are violating the same origin policy. If you control the destination site, you should either consider using a JSONP style of call or enable the cross domain option (crossDomain) on your site and in your AJAX call.

你违反了相同的原产地政策。如果您控制目标站点,您应该考虑使用JSONP样式的调用,或者在站点和AJAX调用中启用跨域选项(cross domain)。

Your browser won't make the call, by default, if you're violating same origin. You'll know that by watching your HTTP traffic using Fiddler, FireBug or Chrome Tools. You won't see your request even executed. If that's the case, then take one of the approaches above.

默认情况下,如果您违反了相同的起源,浏览器不会进行调用。通过使用Fiddler、FireBug或Chrome工具查看HTTP流量,您将了解这一点。您甚至不会看到您的请求被执行。如果是这样,那就采取上面的方法。

I hope this helps. Good luck!

我希望这可以帮助。好运!

#2


3  

The reason is simply cross-domain protection : ajax querying of json from another domain (apparently) can only be done if specific headers are given by the server.

原因是简单的跨域保护:从另一个域(显然是)对json进行ajax查询只能在服务器给出特定的标头的情况下完成。

I suggest you read this complete reference as this is now an essential thing to know in order to code ajax based applications.

我建议您阅读完整的引用,因为要编写基于ajax的应用程序代码,现在必须了解这一点。

Use the same name for both.

两者使用相同的名称。