开发者

JQuery interpreting json as script?

If you all could help me out, I'd really appreciate it.

This is the error I am getting:

"Resource interpreted as Script but transferred with MIME type application/json." ("Resource" is referring to is the json response from google's serv开发者_如何转开发ers.)

Here is my code:

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


Try this instead:

<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>


The site you are calling does not support JSONP, only returns as JSON.


In order to get JSON from another domain (such as googleapis.com), you must use JSONP rather than plain JSON (for more info, read up on same origin policy).

Fortunately, adding a GET parameter whose value is a question mark (callback=?, in your code) causes jQuery to attempt to make a JSONP call. Unfortunately, the site is ignoring your request for JSONP and serving straight JSON.

The two possible causes are that the site doesn't support JSONP (which would be odd for a public Google API) or that it expects the name to be something else (i.e. not callback=). Check out the Google API docs to see what the they expect / support.


So my Maps API is a bit rusty, but doesn't the callback=? at the end turn the output into a JSONP result, which is indeed a script?

Try ditching the callback part of the query string.


Please tell me if this answers your question: webkit based browser interpretate the json as a script

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜