javascript JQuery 不会得到 json?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5281039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:37:46  来源:igfitidea点击:

JQuery won't get json?

javascriptjqueryjson

提问by Andrew

So I'm trying to complete the simple task of getting json data from google, but this little bit of jquery code won't run. Will you please help me figure out why?

所以我试图完成从谷歌获取json数据的简单任务,但是这一点jquery代码不会运行。你能帮我找出原因吗?

  <script src="http://code.jquery.com/jquery-latest.js"></script>

  <script>
  $(document).ready(function(){
    $.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false", function(jsondata) {
            alert(jsondata.status);
    });
  });
  </script>

Best solution: add "&callback=?" to the end of the url. Thank you so much for all your help guys!

最佳解决方案:添加“&callback=?” 到网址的末尾。非常感谢您的帮助!

采纳答案by ctcherry

Its called the Same Origin Policy. In short: the domain that your code is on, is the only domain your javascript can communicate with (by default)

它被称为同源策略。简而言之:您的代码所在的域是您的 javascript 可以与之通信的唯一域(默认情况下)

You get an error like this:

你会收到这样的错误:

XMLHttpRequest cannot load http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin.

回答by Pandincus

Yup, this is absolutely a Same Origin Policy bug.

是的,这绝对是同源策略错误。

It seems that the latest version of the Google Maps API (v3) does not support jsonp. As a result, if you want to geocode, you're going to need to use the maps api:

似乎最新版本的 Google Maps API (v3)不支持 jsonp。因此,如果要进行地理编码,则需要使用 maps api:

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script>
    $(document).ready(function(){
        var loc = "1600 Amphitheatre Parkway, Mountain View, CA";
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode( {'address': loc },
            function(data, status) { console.log(data); });
    });
</script>

Other alternatives:

其他选择:

回答by neebz

Try adding &callback=? to your URL string. It may work.

尝试添加 &callback=? 到您的网址字符串。它可能会起作用。

See this for details > XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

有关详细信息,请参阅此 > XmlHttpRequest 错误:Access-Control-Allow-Origin 不允许 Origin null

回答by Claudiu

I had the same issue. Trying to get json from a server to wich I dind't had access (=> no JSONP).

我遇到过同样的问题。试图从服务器获取 json 到我没有访问权限(=> 没有 JSONP)。

I found http://benalman.com/projects/php-simple-proxy/Add the php proxy to your server and do the ajax call to this file.

我找到了http://benalman.com/projects/php-simple-proxy/将 php 代理添加到您的服务器并对这个文件进行 ajax 调用。

$.ajax({
   type: 'GET',
   url:'proxy.php?url=http://anyDomain.com?someid=thispage',
   dataType: "json",
   success: function(data){
      // success_fn(data);
   },
   error: function(jqXHR, textStatus, errorThrown) {
      // error_fn(jqXHR, textStatus, errorThrown);
   }
});

where proxy.php (the file from Ben Alman) is hosted in your domain

proxy.php(来自 Ben Alman 的文件)托管在您的域中



Alternative (which I found to be second best to this): http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

替代方案(我发现这是第二好的):http: //james.padolsey.com/javascript/cross-domain-requests-with-jquery/

回答by Tengiz

Make sure it's not a cross-domain issue. I guess, for jQuery to be able to call other domain URLs, you need to specify the URL in some special format. I don't exactly remember, but maybe the "?" (question mark) appended in the end of URL?

确保这不是跨域问题。我想,为了让 jQuery 能够调用其他域 URL,您需要以某种特殊格式指定 URL。我不太记得了,但也许是“?” (问号)附加在 URL 的末尾?