jQuery:如何将此URL转换为GET请求

时间:2011-12-06 23:24:15

标签: jquery ajax json rest

我只是搞乱了Factual API但我并不完全理解如何将URL表示为get请求。以下是我想变成jQuery get的URL:

http://api.factual.com/v2/tables/7HOTBC/read?APIKey=SoX1rlj4x8VfRfvqnnehVH5ObpkHJc0kIzloTtxor5gyrHG5c3EySCTTcErCyRYO&filters={"category":{"$bw":"Arts%2C%20Entertainment%20%26%20Nightlife%20%3E%20Bars"},"latitude":{"$blank":false},"longitude":{"$blank":false},"$search":["London"],"$loc":{"$within_dist":[51.5149943,-0.0638818,1000]}}

这是我写的代码:

    var factualKey = "SoX1rlj4x8VfRfvqnnehVH5ObpkHJc0kIzloTtxor5gyrHG5c3EySCTTcErCyRYO";

    $(document).ready(function(){

        // initialise google maps
        initializeGoogleMaps();

        // make a get request to the factual api
        $.get(
                "http://api.factual.com/v2/tables/7HOTBC/read",
                { APIKey : factualKey,
                  filters : { category : { $bw : "Arts, Entertainment & Nightlife > Bars" },
                              latitude : { $blank : false },
                              longitude : { $blank : false },
                              $search : "[\"London\"]",
                              $loc : { $within_dist : [51.5149943,-0.0638818,1000] }
                  }
                },
                function(responseData){
                    alert("SUCCESS");
                },
                "json"
        );
    });

当我查看萤火虫时,这就是我的请求的地址如下:

http://api.factual.com/v2/tables/7HOTBC/read?APIKey=SoX1rlj4x8VfRfvqnnehVH5ObpkHJc0kIzloTtxor5gyrHG5c3EySCTTcErCyRYO&filters[category][%24bw]=Arts%2C+Entertainment+%26+Nightlife+%3E+Bars&filters[latitude][%24blank]=false&filters[longitude][%24blank]=false&filters[%24search]=[%22London%22]&filters[%24loc][%24within_dist][]=51.5149943&filters[%24loc][%24within_dist][]=-0.0638818&filters[%24loc][%24within_dist][]=1000

......这显然是错的!

以下是Factual API的回复:

{"version":"2","status":"error","error":"Your filters parameter cannot be parsed. Please see http:\/\/wiki.developer.factual.com\/Server-API for documentation.","error_type":"Api::Error::InvalidArgument"}

有人可以告诉我我的代码中没有正确完成的操作吗?我是否打算将我传递的数据编码为get请求的一部分?还是我错过了什么?

3 个答案:

答案 0 :(得分:2)

以下是我使用您的对象获得成功查询字符串的方法。

$.param({ APIKey : factualKey,
          filters : JSON.stringify({ category : { $bw : "Arts, Entertainment & Nightlife > Bars" },
                      latitude : { $blank : false },
                      longitude : { $blank : false },
                      $search : "[\"London\"]",
                      $loc : { $within_dist : [51.5149943,-0.0638818,1000] }
          })
});

与您正在进行的操作相同,只是我JSON.stringify过滤器,$.param整个过滤器。

不确定是否需要$.param。可能是jQuery做到了。

也不确定你是否会遇到相同的原始政策问题。

答案 1 :(得分:0)

你应该为jQuery使用适当的jQuery函数:

  

$.getJSON()

     

发送到服务器的数据作为查询附加到URL   串。如果data参数的值是一个对象(map),那就是   转换为字符串并进行url编码后再将其附加到   URL。

随着您的JSON数据越来越严重......

答案 2 :(得分:0)

var factualKey = "SoX1rlj4x8VfRfvqnnehVH5ObpkHJc0kIzloTtxor5gyrHG5c3EySCTTcErCyRYO";

$(document).ready(function(){

    // initialise google maps
    initializeGoogleMaps();

    // make a get request to the factual api
    $.getJSON(
            "http://api.factual.com/v2/tables/7HOTBC/read",
            { APIKey : factualKey,
              filters : JSON.stringify({ category : { $bw : "Arts, Entertainment & Nightlife > Bars" },
                          latitude : { $blank : false },
                          longitude : { $blank : false },
                          $search : "[\"London\"]",
                          $loc : { $within_dist : [51.5149943,-0.0638818,1000] }
              })
            },
            function(data){
                console.log(data);
            }
    );
});