JavaScript POST请求

JavaScript POST请求,javascript,httprequest,Javascript,Httprequest,如何能够启用以下JavaScript函数,发送POST请求,以接收productID参数,而不是作为单个字符串变量,而是作为productID的集合 i、 e.输入输出字符串,如: “productId=126504&productId=126505&productId=126506&productId=126507&productId=126508” 在同一表单上添加多个名称相同的输入项,您应该会得到您想要的内容。在同一表单上添加多个名称相同的输入项,您应该会得到您想要的内容。如果我的问题有点

如何能够启用以下JavaScript函数,发送POST请求,以接收
productID
参数,而不是作为单个字符串变量,而是作为
productID
的集合

i、 e.输入输出字符串,如:

“productId=126504&productId=126505&productId=126506&productId=126507&productId=126508”


在同一表单上添加多个名称相同的输入项,您应该会得到您想要的内容。

在同一表单上添加多个名称相同的输入项,您应该会得到您想要的内容。

如果我的问题有点不清楚,很抱歉。这是该函数的一个变体,我已设法开始按我所希望的方式工作:

    function addToCart3(path, params, method) {
    method = method || "post"; 
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for (var i = 0, l = params.length; i < l; i++ ) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "productId");
        hiddenField.setAttribute("value", params[i]);
        form.appendChild(hiddenField);
    }
    document.body.appendChild(form); 
    form.submit();
}

对不起,如果我的问题有点不清楚。这是该函数的一个变体,我已设法开始按我所希望的方式工作:

    function addToCart3(path, params, method) {
    method = method || "post"; 
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for (var i = 0, l = params.length; i < l; i++ ) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "productId");
        hiddenField.setAttribute("value", params[i]);
        form.appendChild(hiddenField);
    }
    document.body.appendChild(form); 
    form.submit();
}

让它返回一个JSON数组:{productId:[126504126506126507126508]}我不明白你说的“接收”是什么意思。在我看来,这不是一个“javascript”问题,它是HTTP(接近HTML)。让它返回一个JSON数组:{productId:[126504126506126507126508]}我不明白你说的“接收”是什么意思这不是一个“javascript”我想问的是,它是HTTP(接近HTML)。事实上,这就是我想要的——如何使用JavaScript实现它事实上,这就是我想要的——如何使用JavaScript实现它
    function addToCart3(path, params, method) {
    method = method || "post"; 
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for (var i = 0, l = params.length; i < l; i++ ) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "productId");
        hiddenField.setAttribute("value", params[i]);
        form.appendChild(hiddenField);
    }
    document.body.appendChild(form); 
    form.submit();
}
string[] arrstrpostdata = new string[] { "126504", "126505", "126506", "126507", "126508", "126509" };
HtmlPage.Window.Invoke("addToCart3", "http://localhost:10930/Cart/AddCollToCart", arrstrpostdata);