传递多个网址参数为经典ASP [英] Passing multiple URL parameters to Classic ASP
问题描述
编辑:原来我的JavaScript功能不工作,我想,但请与ASP问题,帮助我,而修复中的JavaScript
。 编辑2:
使用警报(JSON.stringify(销售)),看看我得到这个对象:
{712:{KR:1,LR:0},713:{KR:1,LR:0}}
修改3:
需要重新写我的剧本销售,我尝试使用对象,但它似乎并不奏效,因为我没有与JS的这部分很多经验。
变种产品= {
KR:4,
LR:6
};
VAR销售= [];销售功能(ID,产品,价值)
{
this.id =功能PROD(产品价值)
{
this.product =产品;
THIS.VALUE =价值;
}
}销售(N51,KR,0);
希望你能看到我想要做的,但它可能是非常错误的。
和感谢这么久后,这是非常有帮助,我将在所有的,一旦工作作为上述code ++工程
好吧,我有这样的JavaScript脚本,将理清单个图像的销售。
712和713是图象ID。然后KR和LR是产品,它只是将它们添加到URL变量如果真的是为他们销售。
函数submitSales()
{
// URL变量
VAR urlVariable =销售额=?; 对于(销售X)
{
urlVariable = urlVariable + X + - ;
为(S销售[X])
{
如果(销售[X] [S]大于0)
{
urlVariable = urlVariable S +:+销[X] [秒] +,;
}
}
}
urlVariable = urlVariable.substring(0,urlVariable.length - 1);
}
如果我新增销售3产品我被留下了以下字符串:
?销售= 712-KR:1,LR:1713-KR:1
现在什么将首先分离出两个图像销量的最佳途径,所以我将留下:
712-KR:1,LR:1
713-KR:1
然后,我真的需要有他们一个数组,看起来像在:
图片(0)= 712 =产品(0)= KR = 1
产品(1)= LR = 1
图像(1)= 713 =产物(0)= KR = 1
我真的不知道怎么说好,从我读ASP阵列的工作,他们实际上吸。
我真的不知道怎么说好,从我读ASP阵列的工作,他们实际上吸。
块引用>他们这样做,但在这种情况下,也没有关系。
一般的东西
首先,你的JavaScript是错误的。
X
和取值
从来没有宣布,因此它们是全球性的。不这样做,始终以VAR
声明所有变量。- 您没有正确编码您的网址参数。这势必要炸掉,一定要使用正确的编码(
连接codeURIComponent()
在这种情况下)。- 避免依赖于全局变量。通过在
销售
变量作为参数。</ li>- 避免赤裸裸的
的。在
迭代的对象。使用的hasOwnProperty()
作为安全precaution。这是为了抵御该对象的原型一直延续的情况下很好的做法。或者,使用许多JS图书馆的是给你一个通用的每()
的功能之一。 Underscore.js,Sugar.js,jQuery的,都提供这样的功能。或者自己写一个,像我一样的下面。其次,ASP已经非常漂亮的解决了这个问题。
如果您通过具有相同名称的多个参数的查询字符串,服务器把他们集合中的你。 (事实上,
请求
对象的总是的包含集合,那么即使你在一个URL的参数传递只有一次。)所以,当你查询
HTTP://富/酒吧销售= 712-KR%3A1和销售= 712-LR%3A1和销售= 713-KR%3A1的注意EN codeD
:
的然后ASP引擎将提供三种不同的值,在一个集合,像这样的:
712-KR:1,712-LR:1,713-KR:1
- 此集合1为基础,以使
请求(买卖)(1)
将712-KR:1
等。- 您可以使用
请求(买卖)。计数
来找出有多少与有任何给定的名称参数。在这一点上,我建议两件事情。
- 重写你的客户端code,使其发送多个参数的名称相同。
- 切换到字典的上ASP的侧面,而不是试图使用数组。
客户端
JS助手(这实际上可能是不必要的,如果你正在使用的库已经)
函数中的每个(OBJ,回调){
VAR键; 对于(OBJ中键){
如果(obj.hasOwnProperty(键)){
回调(键,OBJ [关键]);
}
}
}JS
函数submitSales(销售){
VAR urlVariable,则params = []; 每个(销售功能(saleId,销售){
每个(销售,功能(的itemId,VAL){
变种参数= saleId + - +的itemId +:+ VAL; 如果(VAL大于0){
params.push(EN codeURIComponent(参数));
}
});
}); urlVariable = + params.join(与&amp;销售=)发售=?; // ...
}服务器
ASP助手
功能NewDict()
设置NewDict =的Server.CreateObject(的Scripting.Dictionary)
结束功能功能计数(元)
如果IsArray的(元)然后
数= UBound函数(元) - LBOUND(元)+ 1
其他
数= vbEmpty
万一
结束功能ASP
功能ParseSales(PARAMS)
昏暗的销售,售后,配件,saleId,价值,产品 集销售= NewDict() 对于每个销售在PARAMS
部分=斯普利特(销售, - )
saleId =零部件(0) 我们要小心,做一个全面的检查
如果Count(部分)= 2,则IsNumeric(saleId)然后
产品=斯普利特(件(1),:) 如果Count(产品)= 2那么
如果不sales.Exists(saleId)然后
sales.Add saleId,NewDict()
万一 销售(saleId)。新增产物(0),产品(1)
其他
无效的参数格式,做出相应的反应
万一
其他
无效的参数格式,做出相应的反应
万一
下一个 设置ParseSales =销售
结束功能现在,你可以做各种事情在服务器端
点心销售,saleId
集销售= ParseSales(请求(销售))回复于sales.Exists(712)' - &GT;真正
回复于sales.Exists(714)' - &GT;假
销售的Response.Write(712),伯爵' - 过夜。; 2
销售的Response.Write(712)(KR)' - &GT; 1
。销售的Response.Write(712)存在(XR)' - &GT;假回复于加入(sales.Keys(),;) - &GT; 712; 713
加入的Response.Write(销售(712)键();。) - &GT; KR; LR对于每个saleId在销售中
saleId将712等,使用销售(saleId)来获得
实际的对象,并用它做什么
下一个Edit: Turns out my JavaScript function is not working as I thought, but please help with the ASP issue whilst I fix the JavaScript.
Edit 2: Using alert(JSON.stringify(sales)) to see the object I get this:
{"712":{"KR":1,"LR":0},"713":{"KR":1,"LR":0}}
Edit 3:
Need to re-write my sales script, i'm trying to use objects but it doesn't seem to be working as I don't have much experience with this part of JS.
var products = { "KR" : 4, "LR" : 6 }; var sale = [ ]; function sale(id,product,value) { this.id = function prod(product,value) { this.product = product; this.value = value; } } sale("N51","KR",0);
Hopefully you can see what i'm trying to do, but it is probably very wrong.
And thanks for that long post, it is very helpful, I will work on all of that as soon as the above code works
Okay, I have this JavaScript script that will sort out the sales for individual images.
712 and 713 is the image ID. Then KR and LR are the products, it only adds them to the URL variable if there is actually a sale for them.
function submitSales() { //URL Variable var urlVariable = "?sales="; for (x in sales) { urlVariable = urlVariable + x + "-"; for (s in sales[x]) { if (sales[x][s] > 0) { urlVariable = urlVariable + s + ":" + sales[x][s] + ","; } } } urlVariable = urlVariable.substring(0, urlVariable.length - 1); }
If I add sales for 3 products I get left with the following string: ?sales=712-KR:1,LR:1,713-KR:1
Now what would the best way of first of all separating the two images sales, so I will be left with:
712-KR:1,LR:1 713-KR:1
Then really I need to have them in a array that would look something like:
image(0) = 712 = product(0) = KR = 1 product(1) = LR = 1 image(1) = 713 = product(0) = KR = 1
I don't really know how ASP array's work that well, from what I read they actually suck.
解决方案I don't really know how ASP array's work that well, from what I read they actually suck.
They do, but in this case it does not matter.
General Stuff
First off, your JavaScript is wrong.
x
ands
are never declared, thus they are global. Don't do this, always declare all variables withvar
.- You are not properly encoding your URL parameters. This is bound to blow up, always use proper encoding (
encodeURIComponent()
in this case).- Avoid relying on global variables. Pass in the
sales
variable as an argument.- Avoid a naked
for .. in
to iterate an object. UsehasOwnProperty()
as a security precaution. This is good practice to defend against the case where the Object prototype has been extended. Alternatively, use one of the many JS librarys that give you a universaleach()
function. Underscore.js, Sugar.js, jQuery, all provide such a function. Or write one yourself, like I did below.Secondly, ASP already solves this problem very nicely.
If you pass multiple parameters with the same name in your query string, the server puts them in a collection for you. (In fact, the
Request
object always contains collections, even if you pass in an URL-parameter only once.)So when you query
http://foo/bar?sale=712-KR%3A1&sale=712-LR%3A1&sale=713-KR%3A1Note the encoded
:
then the ASP engine will deliver three distinct values, in one collection, like this:
"712-KR:1", "712-LR:1", "713-KR:1"
- This collection is 1-based, so that
Request("sale")(1)
will be"712-KR:1"
and so on.- You can use
Request("sale").Count
to find out how many parameters with any given name there are.At this point I suggest two things.
- Rewrite your client code so that it sends multiple parameters with the same name.
- Switch to dictionaries on the ASP side instead of trying to use arrays.
Client
js Helper (this might actually be unnecessary if you are using a library already)
function each(obj, callback) { var key; for (key in obj) { if (obj.hasOwnProperty(key)) { callback(key, obj[key]); } } }
js
function submitSales(sales) { var urlVariable, params = []; each(sales, function (saleId, sale) { each(sale, function (itemId, val) { var param = saleId + "-" + itemId + ":" + val; if (val > 0) { params.push( encodeURIComponent(param) ); } }); }); urlVariable = "?sale=" + params.join("&sale="); // ... }
Server
ASP helpers
Function NewDict() Set NewDict = Server.CreateObject("Scripting.Dictionary") End Function Function Count(ary) If IsArray(ary) Then Count = UBound(ary) - LBound(ary) + 1 Else Count = vbEmpty End If End Function
ASP
Function ParseSales(params) Dim sales, sale, parts, saleId, value, product Set sales = NewDict() For Each sale In params parts = Split(sale, "-") saleId = parts(0) ' let's be careful and do a sanity check If Count(parts) = 2 and IsNumeric(saleId) Then product = Split(parts(1), ":") If Count(product) = 2 Then If Not sales.Exists(saleId) Then sales.Add saleId, NewDict() End If sales(saleId).Add product(0), product(1) Else ' Invalid parameter format, react accordingly End If Else ' Invalid parameter format, react accordingly End If Next Set ParseSales = sales End Function
Now you can do all kinds of things on the server side
Dim sales, saleId Set sales = ParseSales(Request("sales")) Response.Write sales.Exists("712") ' -> True Response.Write sales.Exists("714") ' -> False Response.Write sales("712").Count ' -> 2 Response.Write sales("712")("KR") ' -> 1 Response.Write sales("712").Exists("XR") ' -> False Response.Write Join(sales.Keys(), "; ") ' -> 712; 713 Response.Write Join(sales("712").Keys(), "; ") ' -> KR; LR For Each saleId In sales ' saleId will be "712" and so on, use sales(saleId) to get the ' actual object and do something with it Next
这篇关于传递多个网址参数为经典ASP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!