The most common way to do this is to:
Copy Code code as follows:
Url?arg1=value1&arg2=value2&arg3=value3 ...
Such a way is most common and easiest to understand, but in a project, if the parameters that follow are variable and the fields have different values or different semantics, such a way of maintainability and readability is actually not high, and the code is easy to repeat or redundant.
For example, the following is a request string for CGI:
Copy Code code as follows:
var url = "http://www.tenpay.com/app/v1.0/juhui.cgi?";
var querystring = "Method=2&page=index";
if (content a) {//access to the first page of the area A, need to add parameters subpage
QueryString + = "&subpage=a";
}else if (content B) {//if area B is accessed, parameter subpage to B
QueryString + = "&subpage=b";
}
if (Spec_method) {
If you want to follow the specified filtering method when viewing, you also need to add a parameter Spec_method
QueryString = "&spec_method=1"
}
This is the most common piece of string logic, such code is not a problem, but write comments is cumbersome, and not very readable, the field description is not clear, if you need to replace a field or change logic on the original basis is more cumbersome.
I see some of my colleagues ' solutions to this problem, the first is to save the parameters in the form of objects, and then write a method to spell the parameters at the request:
Copy Code code as follows:
var queryconfig={
"page": "Index",
"Methods": 2,//1: Follow Method A view 2: View by Method B
"Subpage":-1,//-1: This condition does not pass a: view Contenta B: View CONTENTB
"Spec_method":-1//-1: This condition does not pass 1: According to the sales height check 2: According to the time check
};
var setqueryconfig = function () {
var _str = "";
for (Var o in queryconfig) {
if (Queryconfig[o]!=-1) {
_str + = O + "=" + Queryconfig[o] + "&";
}
}
var _str = _str.substring (0, str.length-1);
return _str;
}
This method is very good, the advantage is that all the parameters at a glance in the object of all columns, comments can be more detailed for the field, readability and maintenance are improved; But the disadvantage is that the code is a little bit more, but also need a special method to combine parameters.
Another method is to use an array method:
Copy Code code as follows:
var querystring = [
"method=2", Notes for//method fields
"Page=index"
];
if (content a) {//access to the first page of the area A, need to add parameters subpage
Querystring.concat ([
"Subpage=a",//subpage annotation
]);
}else if (content B) {//if area B is accessed, parameter subpage to B
Querystring.concat ([
"Subpage=b",//subpage annotation
]);
}
if (Spec_method) {
If you want to follow the specified filtering method when viewing, you also need to add a parameter Querystring.concat ([
"spec_method=2",//spec_method annotation
]);
}
QueryString = Querystring.join ("&");
This method may be less readable than the object's method, but it also has a high maintenance and a small amount of code. I still like this method comparatively.
Well, if it weren't for yesterday's code review, I'd have to work long enough to find code optimization in such a small place. It seems that the code review on their own ability to improve is accelerator acridine, haha.
These two methods I found temporarily, if the follow-up find any good method, and then add in ~