如何使用 angular 在 ionic 4 的 http URL 请求中传递变量?
How can a variable be passed in http URL request in ionic 4 with angular?
如何使用 angular 在 ionic 4 的 http 请求中将变量作为参数传递?
response: any;
constructor(private http: HttpClient) { }
async logIn(Companyid: string, Userid: string, Password: string, lat: any, lot: any, deviceid: string) {
console.log(Companyid, Userid, Password);
this.http.get('https://www.service.com/App_1.3.5/login.php',
)
.subscribe(data => {
this.response = data;
});
// console.log(this.response);
// return this.response
}
}
我试过如下但它作为字符串传递
export class UrlService {
response: any;
constructor(private http: HttpClient) { }
async logIn(Companyid: string, Userid: string, Password: string,
lat: any, lot: any, deviceid: string) {
// tslint:disable-next-line:whitespace
// const httOptions = ;
console.log(Companyid, Userid, Password);
// tslint:disable-next-line:max-line-length
this.http.get('https://www.service.com/App_1.3.5/login.php?compid=Companyid&username=Userid&password=Password&deviceID=deviceid&latitude=lat&longitude=lot')
.subscribe(data => {
this.response = data;
});
// console.log(this.response);
// return this.response
}
}
所以请帮助我如何在 http URL 请求中将变量作为参数传递
您正在直接传递字符串而不是引用变量,试试这个:
this.http.get(`https://www.service.com/App_1.3.5/login.php?compid=${Companyid}&username=${Userid}&password=${Password}&deviceID=${deviceid}&latitude=${lat}&longitude=${lot}`)
.subscribe(data => {
this.response = data;
});
// console.log(this.response);
// return this.response
}
Note: Notice that ` is not a single quotation and its tilde in
keyboard
您可以在将其传递给您的请求之前使用参数构建 url:
const baseURL = 'https://www.service.com/App_1.3.5/login.php?';
const queryOptions = 'compid=' + Companyid + '&username=' + Userid; //Keep going
let requestUrl = `${baseURL}${queryOptions}`;
this.http.get(requestUrl)
.subscribe(data => {
this.response = data;
});
}
例如,您有 LoginPage 现在转到 login.page.ts 文件并编写以下代码:
class LoginPage {
constructor(private loginService: LoginService)
async login(){
this.loginService.userLogin(companyid,userId,password,latitude,longitude,deviceId)
.then((data)=>{
console.log(data)
},(err)=>{
console.log('error',err);
})
}
}
现在去你的服务,写下面的代码
async userLogin(companyId,userId,password,lat,log,deviceId){
const url = 'https://www.service.com/App_1.3.5/login.php';
let params: any = {
companyid: companyId,
userid: userId,
password: password,
latitude:lat,
longitude:log,
deviceId: deviceId
};
return this.httpClient
.get(url, params)
.then(data => data);
}
我想这会解决你的问题。
如何使用 angular 在 ionic 4 的 http 请求中将变量作为参数传递?
response: any;
constructor(private http: HttpClient) { }
async logIn(Companyid: string, Userid: string, Password: string, lat: any, lot: any, deviceid: string) {
console.log(Companyid, Userid, Password);
this.http.get('https://www.service.com/App_1.3.5/login.php',
)
.subscribe(data => {
this.response = data;
});
// console.log(this.response);
// return this.response
}
}
我试过如下但它作为字符串传递
export class UrlService {
response: any;
constructor(private http: HttpClient) { }
async logIn(Companyid: string, Userid: string, Password: string,
lat: any, lot: any, deviceid: string) {
// tslint:disable-next-line:whitespace
// const httOptions = ;
console.log(Companyid, Userid, Password);
// tslint:disable-next-line:max-line-length
this.http.get('https://www.service.com/App_1.3.5/login.php?compid=Companyid&username=Userid&password=Password&deviceID=deviceid&latitude=lat&longitude=lot')
.subscribe(data => {
this.response = data;
});
// console.log(this.response);
// return this.response
}
}
所以请帮助我如何在 http URL 请求中将变量作为参数传递
您正在直接传递字符串而不是引用变量,试试这个:
this.http.get(`https://www.service.com/App_1.3.5/login.php?compid=${Companyid}&username=${Userid}&password=${Password}&deviceID=${deviceid}&latitude=${lat}&longitude=${lot}`)
.subscribe(data => {
this.response = data;
});
// console.log(this.response);
// return this.response
}
Note: Notice that ` is not a single quotation and its tilde in keyboard
您可以在将其传递给您的请求之前使用参数构建 url:
const baseURL = 'https://www.service.com/App_1.3.5/login.php?';
const queryOptions = 'compid=' + Companyid + '&username=' + Userid; //Keep going
let requestUrl = `${baseURL}${queryOptions}`;
this.http.get(requestUrl)
.subscribe(data => {
this.response = data;
});
}
例如,您有 LoginPage 现在转到 login.page.ts 文件并编写以下代码:
class LoginPage {
constructor(private loginService: LoginService)
async login(){
this.loginService.userLogin(companyid,userId,password,latitude,longitude,deviceId)
.then((data)=>{
console.log(data)
},(err)=>{
console.log('error',err);
})
}
}
现在去你的服务,写下面的代码
async userLogin(companyId,userId,password,lat,log,deviceId){
const url = 'https://www.service.com/App_1.3.5/login.php';
let params: any = {
companyid: companyId,
userid: userId,
password: password,
latitude:lat,
longitude:log,
deviceId: deviceId
};
return this.httpClient
.get(url, params)
.then(data => data);
}
我想这会解决你的问题。