Angular上的HTTP请求中不包含特定参数

时间:2017-09-03 13:59:42

标签: angular typescript ionic2

我在Angular和Ionic 2中有一个奇怪的问题。我有以下提供者:

  getTransportations = function (category, maxResults) {    
    let url = `${this.domain}/wp-json/gf/v2/forms/1/entries?_labels=1&paging[page_size]=${maxResults}`;
    if (category !== 'all') {
      let search = {
        field_filters: [{
          key: '2',
          operator: 'is',
          value: category
        }]
      };
      let url = `${this.domain}/wp-json/gf/v2/forms/1/entries?_labels=1&paging[page_size]=${maxResults}&search=${encodeURI(JSON.stringify(search))}`;
      console.log(url);

    }
    let headers: Headers = new Headers();
    headers.append("Authorization", "Basic " + this.bt);
    headers.append("Content-type", "application/json");
    return this.http.get(url, {
        headers: headers
      })
      .map(response => response.json())

  };

如果类别为all,则提供商按预期工作。但是,如果该类别是其他内容,则在Chrome上的开发工具中,在网络标签中,我可以看到没有搜索参数的被调用网址。只需_labelspaging。但是,我在if中添加了console.log并打印了正确的URL,如果我在Postman上使用它,我会得到我想要的过滤结果。

我尝试了不同的方法来连接字符串,但我一直有同样的问题。

1 个答案:

答案 0 :(得分:1)

存在范围问题。 let url = `${this.domain}/wp-json/gf/v2/forms/1/entries?_labels=1&paging[page_size]=${maxResults}&search=${encodeURI(JSON.stringify(search))}`; 是块范围的。

if (...) { ... }

的范围属于url。这两个if是不同的变量,并且从未使用console.log中分配的变量(url = `${this.domain}/wp-json/gf/v2/forms/1/entries?_labels=1&paging[page_size]=${maxResults}&search=${encodeURI(JSON.stringify(search))}`; 除外)。

应该是

COLLATE "C