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

时间:2021-10-02 19:42:55

I am trying to pass url args to http get request in angular 2 but getting an error in the console:

我试图在URL 2中将url args传递给http get请求,但在控制台中收到错误:

InstantiationError {_wrapperMessage: "DI Exception", _originalException: TypeError: options.search.clone is not a function at BaseRequestOptions.RequestOptions.merge....

InstantiationError {_wrapperMessage:“DI异常”,_原始异常:TypeError:options.search.clone不是BaseRequestOptions.RequestOptions.merge中的函数....

here's the code

这是代码

Service

  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;
              });
      }
});

Component

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 个解决方案

#1


2  

I would use the following:

我会使用以下内容:

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

The URLSearchParams object is defined under "ng.http.".

URLSearchParams对象在“ng.http。”下定义。

#1


2  

I would use the following:

我会使用以下内容:

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

The URLSearchParams object is defined under "ng.http.".

URLSearchParams对象在“ng.http。”下定义。