json - 不将参数传递给 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 个参数
解决方案
http.get()
最多接受 2 个参数:url 和由 headers 组成的 RequestOptions。
如果要在字符串中添加 url 参数,请使用反引号:
this.http.get(`http://localhost:8100/Service1.svc/RestService/Checkstudent/${stuid}/${subid}`)
这称为模板文字
推荐阅读
- c++ - 如何在 cmake 中启用`/std:c++latest`?
- javascript - “#document”类型的节点不能插入“HEAD”类型的节点内
- javascript - 如何从这个 api 中选择我需要的数据?
- angular - chartOptions.redraw 不是使用角度的 highcharts 中的函数
- ios - SwiftUI 中的 AVSampleBufferDisplayLayer 在 UIViewRepresentable
- elasticsearch - 如何从别名或索引名称中获取索引类型?
- firebase - 达到云功能的最大实例数时的 Firestore 触发行为
- java - 使用 Streams.zip 方法同时为 LocalDate 对象使用两个流
- javascript - JavaScript 根据数据组合多个数组
- javascript - 如何让选中的复选框在警报时显示