【问题标题】:How do I pass an URL via Angular?如何通过 Angular 传递 URL?
【发布时间】:2018-10-15 19:14:29
【问题描述】:

这是我正在尝试做的,但我知道 URL 中的斜杠不正确

http://localhost:57101/api/employee/holiday?userName=dinchmle&Date=05/10/2018&StateVal=2

这是我的角度服务代码

 updateEmployee(userName: string, holiday: Holiday): Observable<Employee> {
    return this.http.put(this._employeeUrl + '/holiday?userName=' + userName + '&Date=' + holiday.date + '&StateVal=' + holiday.state, '')
    .map((res: Employee) => res)
    .catch(this.handleError);
  }

【问题讨论】:

  • 有什么难度?

标签: angular url


【解决方案1】:

只需使用encodeURI 函数对您的网址进行编码。这将包含您 URL 中的所有特殊字符。

 updateEmployee(userName: string, holiday: Holiday): Observable<Employee> {
    let url = this._employeeUrl + '/holiday?userName=' + userName + '&Date=' + holiday.date + '&StateVal=' + holiday.state;

    return this.http.put(encodeURI(url), '') // encode URL
    .map((res: Employee) => res)
    .catch(this.handleError);
  }

【讨论】:

    【解决方案2】:

    您可以将斜杠转义为%2F

    这是编辑后的示例:

    updateEmployee(userName: string, holiday: Holiday): Observable<Employee> {
        let parsedDate = Date.parse(holiday.date);
        let month = parsedDate.getUTCMonth() + 1; //months from 1-12
        let day = parsedDate.getUTCDate();
        let year = parsedDate.getUTCFullYear();
    
        return this.http.put(this._employeeUrl + '/holiday?userName=' + userName + '&Date=' + day + '%2F' + month + '%2F' + year + '&StateVal=' + holiday.state, '')
            .map((res: Employee) => res)
            .catch(this.handleError);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-25
      • 2016-09-30
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多