开发者

JSONRequest.get unresponsive

I am trying to send get requests to the Google Places API with this code:

<script type="text/javascript" src="json2.js">
var googleQuery;
function load() {
    googleQuery = JSONRequest.get(  
       "https://maps.googleapis.com/maps/api/place/details/json?reference=3af0d044d45cd8587d9a3522bc98a95d4f60c6a8&sensor=true&key=xxxxxxxxxxxxxxxx开发者_如何学C",
       function (googleQuery, value, exception) {
          if (value) {
             processResponse(value);
          }
          else {
             processError(exception);
          }
       }
    ); 
}
</script>

And calling the load function in the body onload.

  <body onload="load()">
  </body>

I am including the src="json2.js" in this <script> instead of in its own <script>, since I was getting a "JSONRequest is undefined" error...but I am still getting a strange "load is undefined" error.

Am I going about this JSON request correctly?


Try:

<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript">
    var googleQuery;
    function load(){
    googleQuery = JSONRequest.get(  
        "https://maps.googleapis.com/maps/api/place/details/json?reference=3af0d044d45cd8587d9a3522bc98a95d4f60c6a8&sensor=true&key=xxxxxxxxxxxxxxxx",
     function (googleQuery, value, exception) {
        if (value) {
            processResponse(value);
        } else {
            processError(exception);
        }
      }
    ); 
   }
</script>

You can't have JavaScript code inside a script tag which has the src attribute. You should place the inline code on another script tag, otherwise it won't be executed.


Replace the line :

<script type="text/javascript" src="json2.js">

with

<script type="text/javascript" src="json2.js"></script>
<script>

Your JS code is being ignored since you specified an src attribute.


JSONRequest is more so just a proposal which browsers can implement at their will (I think Firefox does).

I'm not sure if there are any libraries that can be script sourced to in order to use JSONRequest, but an alternative is to use flyJSONP (http://alotaiba.github.com/FlyJSONP/#!/demo).

flyJSONP uses YQL (Yahoo Query Language) to do any cross domain post/get, and I highly recommend it (especially for google api's such as oAuth 2.0 and ClientLogin)... and it has a debugger mechanism.

Also, there is jankyPost (http://saunter.org/janky.post/). I have not used it but I'm sure I will and I like its concept. Its kinda clugy, or well... janky... but read about how it works (short paragraph) and you'll love it and want to build you own perhaps.

--Cody

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜