【问题标题】:Not passing Parameter to URL in IONIC不将参数传递给 IONIC 中的 URL
【发布时间】:2018-04-24 11:42:29
【问题描述】:

我正在尝试从主页获取参数这是表单代码。数据已经传递给函数(选择)。

<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 个参数,但得到 3 个 符合 this.http.get("http://localhost:8100/Service1.svc/RestService/Checkstudent/{0}/{1}",stuid,subid)

【问题讨论】:

标签: json forms url ionic-framework get


【解决方案1】:

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

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

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

这叫Template Literal

【讨论】:

  • 哇,它的工作 Broooo :D 非常感谢。我花了 5 个小时来弄清楚谢谢:)
  • 用你的代码我只是把这个`替换成'这个然后它不起作用。只需替换您的代码即可! .我不明白你能解释一下吗
  • 一个是单引号,另一个是反引号。转义键下方的键。它的 js 语法用于在字符串文字中嵌入变量。检查答案中的链接
  • 谢谢 :) 我明白了 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
相关资源
最近更新 更多