根据用户输入在 javascript 中动态创建 URL
Dynamically creating URL in javascript based on user input
我正在尝试根据用户选择动态创建 URL
所以我这样写js:
getPatientProfile(patient, relative, relation, contactNumber, townCity) {
var p = patient.trim()
var r = relative.trim()
var url = 'http://192.168.1.3/api/clinic/patient/profile?patient=' + p + '&relative=' + r ;
if (relation != null || relation != "" || relation != undefined){
url += "&relationType=" + relation;
}
if (contactNumber != null || contactNumber != ""){
url += "&contactNumber=" + contactNumber;
}
if (townCity != null || townCity != ""){
url += "&townCity=" + townCity;
}
return axios.get(url);
}
但我还是得到了整个 URL: http://192.168.1.3/api/clinic/patient/profile?patient=vernacular&relative=Dreams&relationType=undefined&contactNumber=&townCity=
如果用户没有提供关系、contactNumber 和 townCity,我想要什么 url 应该只是 http://192.168.1.3/api/clinic/patient/profile?patient=vernacular&relative=Dreams
当您希望所有条件都为真时,您需要使用 &&
。
或者您甚至不需要显式检查所有虚假值
function getPatientProfile(patient, relative, relation, contactNumber, townCity) {
var p = patient.trim()
var r = relative.trim()
var url = 'http://192.168.1.3/api/clinic/patient/profile?patient=' + p + '&relative=' + r ;
if (relation){
url += "&relationType=" + relation;
}
if (contactNumber){
url += "&contactNumber=" + contactNumber;
}
if (townCity){
url += "&townCity=" + townCity;
}
return url
}
console.log(getPatientProfile('rel','pat'))
当用户没有给出任何值时,它总是条件为真。
townCity != null 和 relation != null 和 contactNumber != null
function getPatientProfile(patient, relative, relation, contactNumber, townCity)
{
var p = patient.trim()
var r = relative.trim()
var url = 'http://192.168.1.3/api/clinic/patient/profile?patient=' + p + '&relative=' + r ;
if (relation != null && relation != "" && relation != undefined){
url += "&relationType=" + relation;
}
if (contactNumber != null && contactNumber != ""){
url += "&contactNumber=" + contactNumber;
}
if (townCity != null && townCity != ""){
url += "&townCity=" + townCity;
}
return axios.get(url);
}
如果您采用 json 方式,您可以使 getPatientProfile(patient, relative, relation, contactNumber, townCity)
更通用:
var keys = {
patient : "Thor",
relative : "Loki",
relation : "Brothers",
contactNumber : null,
townCity : ""
};
function getPatientProfile(options){
var url = 'http://192.168.1.3/api/clinic/patient/profile';
for (key in options){
var separator = url.indexOf('?') !== -1 ? "&" : "?";
if(!!options[key]){ //checks for the truthy condition of the options[key]
options[key] = options[key].trim(); //you can also add conditions for the keys which you want to trim.
url += separator + key + "=" + options[key];
}
}
return url;
}
这样你就可以轻松地 add/remove 键:)
我正在尝试根据用户选择动态创建 URL
所以我这样写js:
getPatientProfile(patient, relative, relation, contactNumber, townCity) {
var p = patient.trim()
var r = relative.trim()
var url = 'http://192.168.1.3/api/clinic/patient/profile?patient=' + p + '&relative=' + r ;
if (relation != null || relation != "" || relation != undefined){
url += "&relationType=" + relation;
}
if (contactNumber != null || contactNumber != ""){
url += "&contactNumber=" + contactNumber;
}
if (townCity != null || townCity != ""){
url += "&townCity=" + townCity;
}
return axios.get(url);
}
但我还是得到了整个 URL: http://192.168.1.3/api/clinic/patient/profile?patient=vernacular&relative=Dreams&relationType=undefined&contactNumber=&townCity=
如果用户没有提供关系、contactNumber 和 townCity,我想要什么 url 应该只是 http://192.168.1.3/api/clinic/patient/profile?patient=vernacular&relative=Dreams
当您希望所有条件都为真时,您需要使用 &&
。
或者您甚至不需要显式检查所有虚假值
function getPatientProfile(patient, relative, relation, contactNumber, townCity) {
var p = patient.trim()
var r = relative.trim()
var url = 'http://192.168.1.3/api/clinic/patient/profile?patient=' + p + '&relative=' + r ;
if (relation){
url += "&relationType=" + relation;
}
if (contactNumber){
url += "&contactNumber=" + contactNumber;
}
if (townCity){
url += "&townCity=" + townCity;
}
return url
}
console.log(getPatientProfile('rel','pat'))
当用户没有给出任何值时,它总是条件为真。 townCity != null 和 relation != null 和 contactNumber != null
function getPatientProfile(patient, relative, relation, contactNumber, townCity)
{
var p = patient.trim()
var r = relative.trim()
var url = 'http://192.168.1.3/api/clinic/patient/profile?patient=' + p + '&relative=' + r ;
if (relation != null && relation != "" && relation != undefined){
url += "&relationType=" + relation;
}
if (contactNumber != null && contactNumber != ""){
url += "&contactNumber=" + contactNumber;
}
if (townCity != null && townCity != ""){
url += "&townCity=" + townCity;
}
return axios.get(url);
}
如果您采用 json 方式,您可以使 getPatientProfile(patient, relative, relation, contactNumber, townCity)
更通用:
var keys = {
patient : "Thor",
relative : "Loki",
relation : "Brothers",
contactNumber : null,
townCity : ""
};
function getPatientProfile(options){
var url = 'http://192.168.1.3/api/clinic/patient/profile';
for (key in options){
var separator = url.indexOf('?') !== -1 ? "&" : "?";
if(!!options[key]){ //checks for the truthy condition of the options[key]
options[key] = options[key].trim(); //you can also add conditions for the keys which you want to trim.
url += separator + key + "=" + options[key];
}
}
return url;
}
这样你就可以轻松地 add/remove 键:)