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

jQuery:如何将此URL转换为GET请求,jquery,ajax,json,rest,Jquery,Ajax,Json,Rest,我只是在摆弄实际的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%

我只是在摆弄实际的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"
        );
    });
当我查看firebug时,我的请求地址是这样的:

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
…这显然是错误的

下面是来自真实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请求一部分传递的数据进行编码?还是我遗漏了什么?

您应该为该作业使用jQuery的适当函数:

发送到服务器的数据作为查询附加到URL 一串如果数据参数的值是对象(贴图),则为 转换为字符串,并在将其附加到 网址


当您的JSON数据被破坏时…

以下是我如何使用您的对象成功获得查询字符串的

$.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做的

也不确定您是否会遇到同源策略问题


那么$.get是否也对字符串进行url编码?因为看起来这正是我缺少的…请尝试通过
$.param()
发送数据
$.param({APIKey:factualKey,filt…})
…不管怎样,我得到的错误与我这样做时得到的错误相同。我认为您需要将过滤器:转换为JSON字符串,然后对其进行URL编码。现在,jQuery似乎正在尝试使用括号-->过滤器[category][$bw]=etc来保留对象。。。您真正想要的是-->&filters={url编码的json数据没有被jQuery参数劫持(sp?}
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);
            }
    );
});