【问题标题】:jQuery: How to turn this URL into a GET requestjQuery:如何将此 URL 转换为 GET 请求
【发布时间】:2011-12-06 23:24:14
【问题描述】:

我只是在搞乱 Factual API,但我不完全了解如何将 URL 表示为获取请求。以下是我要转成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 请求的一部分传递的数据进行编码?还是我错过了什么?

【问题讨论】:

  • 尝试通过$.param() 发送您的数据。 $.param({APIKey:factualKey, filt...})
  • ...没关系,当我这样做时,我会遇到与您相同的错误。
  • 我认为您需要将您的过滤器 : 转换为 JSON 字符串,然后对其进行 urlencode。现在看起来 jQuery 正在尝试使用方括号来保留对象 --> filter[category][$bw]=etc... 而您真正想要的是 --> &filters={ url 编码的 json 数据没有被劫持通过 jQuery 参数化 (sp?) }

标签: jquery ajax json rest


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    $.getJSON()

    发送到服务器的数据作为查询附加到 URL 细绳。如果data参数的值是一个对象(map),它是 在附加到 网址。

    随着您的 JSON 数据被破坏...

    【讨论】:

    • $.get 也对字符串进行 url 编码吗?因为看起来这就是我所缺少的......
    【解决方案3】:
    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);
                }
        );
    });
    

    【讨论】:

      猜你喜欢
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2019-11-04
      • 1970-01-01
      • 2015-06-24
      • 2012-04-02
      • 1970-01-01
      相关资源
      最近更新 更多