gpt4 book ai didi

javascript - 将多个 URL 参数传递给经典 ASP

转载 作者:行者123 更新时间:2023-11-28 08:59:17 24 4
gpt4 key购买 nike

编辑:原来我的 JavaScript 函数没有像我想象的那样工作,但请在我修复 JavaScript 时帮助解决 ASP 问题。

编辑 2:使用alert(JSON.stringify(sales))来查看我得到的对象:

{"712":{"KR":1,"LR":0},"713":{"KR":1,"LR":0}}

编辑3:

需要重写我的销售脚本,我正在尝试使用对象,但它似乎不起作用,因为我对这部分 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);

希望您能明白我正在尝试做的事情,但这可能是非常错误的。

感谢这篇长文章,它非常有帮助,一旦上述代码起作用,我将立即处理所有这些问题

<小时/>

好的,我有这个 JavaScript 脚本,可以对单个图像的销售情况进行排序。

712和713是图像ID。那么 KR 和 LR 就是产品,只有当它们确实有销售时,它才会将它们添加到 URL 变量中。

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);
}

如果我添加 3 种产品的销售额,我会得到以下字符串:?销售=712-KR:1,LR:1,713-KR:1

现在,首先将两个图像销售分开的最佳方法是什么,所以我将剩下:

712-KR:1,LR:1713-KR:1

然后我真的需要将它们放在一个看起来像这样的数组中:

image(0) = 712 = product(0) = KR = 1
product(1) = LR = 1
image(1) = 713 = product(0) = KR = 1

我真的不知道 ASP 数组是如何工作得那么好,从我读到的来看,它们实际上很糟糕。

最佳答案

I don't really know how ASP array's work that well, from what I read they actually suck.

确实如此,但在这种情况下并不重要。

一般内容

首先,你的 JavaScript 是错误的。

  • xs 从未声明过,因此它们是全局的。不要这样做,始终使用 var 声明所有变量。
  • 您没有正确编码 URL 参数。这肯定会崩溃,请始终使用正确的编码(在本例中为 encodeURIComponent())。
  • 避免依赖全局变量。传入 sales 变量作为参数。
  • 避免使用裸露的 for .. in 来迭代对象。使用 hasOwnProperty() 作为安全预防措施。这是防御对象原型(prototype)已扩展的情况的良好实践。或者,使用为您提供通用 each() 函数的众多 JS 库之一。 Underscore.js、Sugar.js、jQuery,都提供了这样的功能。或者你自己写一个,就像我在下面所做的那样。

其次,ASP已经很好地解决了这个问题。

如果您在查询字符串中传递多个同名参数,服务器会为您将它们放入一个集合中。 (事实上​​,Request 对象始终包含集合,即使您只传递一次 URL 参数。)

所以当你查询时

http://foo/bar?sale=712-KR%3A1&sale=712-LR%3A1&sale=713-KR%3A1

Note 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"
  • 此集合是从 1 开始的,因此 Request("sale")(1) 将是 "712-KR:1" 等等。
  • 您可以使用 Request("sale").Count 来找出具有任意给定名称的参数的数量。

此时我建议两件事。

  • 重写您的客户端代码,以便它发送多个具有相同名称的参数。
  • 切换到dictionaries在 ASP 端而不是尝试使用数组。

客户端

js Helper(如果您已经使用库,这实际上可能是不必要的)

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=");

// ...
}

服务器

ASP 助手

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

现在您可以在服务器端执行各种操作

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

关于javascript - 将多个 URL 参数传递给经典 ASP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17975148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com