首页 > 解决方案 > 不将参数传递给 IONIC 中的 URL

问题描述

我试图从主页获取参数这是表单 CODE.Data 已经传递给函数(选择)。

<form #f="ngForm" (ngSubmit)="Select(f.value)">
 <ion-input placeholder="Student ID" type="text" name="Studentid" ngModel></ion-input>
<ion-input placeholder="Subject ID" type="text" name="Subjectid" ngModel></ion-input>
<input type="submit"/>
</form>

这是我的 ts 代码

export class HomePage {

users:any;
  constructor(public navCtrl: NavController,public http :Http) {
  }

这是我的功能。这些值是从表单传递的,而不是传递给 url 参数,但在 console.log 中的值正在打印。

Select(data) {

      var stuid= data.Studentid;
      var subid = data.Subjectid;
if(stuid && subid != null)
{
  console.log(stuid);
  console.log(subid);

      this.http.get("http://localhost:8100/Service1.svc/RestService/Checkstudent/{0}/{1}",stuid,subid)
      .map(res => res.json())
      .subscribe(res => {
        this.users = res;
      }, (err) => {
        alert("failed loading json data");
      });
}
    }

这是网络日志中执行函数的结果

请求网址: http://localhost:8100/Service1.svc/RestService/Checkstudent/%7B0%7D/%7B1%7D

这个错误在开始时也如此显示

这个错误也在开始时显示

预期有 1-2 个参数,但在 this.http.get(" http://localhost:8100/Service1.svc/RestService/Checkstudent/ {0}/{1}",stuid,subid)中有 3 个参数

标签: jsonformsurlionic-frameworkget

解决方案


http.get()最多接受 2 个参数:url 和由 headers 组成的 RequestOptions。

如果要在字符串中添加 url 参数,请使用反引号:

this.http.get(`http://localhost:8100/Service1.svc/RestService/Checkstudent/${stuid}/${subid}`)

这称为模板文字


推荐阅读