将URL参数传递给角度为2的http get请求

时间:2016-04-08 06:52:39

标签: javascript http angular

我正在尝试将url args传递给角度2中的http get请求但在控制台中出错:

  

InstantiationError {_wrapperMessage:“DI Exception”,   _originalException:TypeError:options.search.clone不是函数       在BaseRequestOptions.RequestOptions.merge ....

这是代码

服务

  var url = '/company/' + companyUnqName + '/logs' ;  
  app.AuditLogsHttpService = ng.core.Injectable().Class({
      constructor: [ng.http.Http,  function(http){
        this.http = http
      }],

      getLogs: function(){
        var params = new URLSearchParams();
        params.set('uniquepage', 1);
        return this.http.get(url, {search: params}) 
              .map(function (res) {
                  return res;
              });
      }
});

组件

app.AuditLogs =
  ng.core.Component({
    selector: 'audit-logs',
    providers: [ng.http.HTTP_PROVIDERS, httpService],
    templateUrl: '/public/webapp/ng2/audit_logs/audit-logs.html'
  })
  .Class({
    constructor:[ng.http.Http, httpService, function(http, service) {
      this.result = {};
      this.http = http;
      service.getLogs().subscribe(function(result){
        this.result = JSON.parse(result._body);
        this.logs = this.result.body.logs;
      }.bind(this));
    }],
});

1 个答案:

答案 0 :(得分:2)

我会使用以下内容:

getLogs: function(){
     var params = new ng.http.URLSearchParams(); // <--------
  params.set('uniquepage', 1);
  return this.http.get(url, {search: params})   
          .map(function (res) {
            return res;
          });
}

URLSearchParams对象在&#34; ng.http。&#34;。

下定义。