简体   繁体   English

是否可以使用javascript通过POST多次发送一个变量?

[英]Is it possible sending one variable multiple time via POST using javascript?

Let me explain why I need to do this! 让我解释一下为什么我需要这样做!

I need to send a request to another server that its format is something like this: 我需要向另一台服务器发送一个请求,其格式是这样的:

http://www.test.ccom/process?item=1AAA&item=2BBB&item=3CCC HTTP://www.test.ccom/process项目= 1AAA及项目= 2BBB及项目= 3CCC

This URL will add 3 different items (one of each) to the result page, like this: 此URL将向结果页面添加3个不同的项目(每个项目一个),如下所示:

Item = 1AAA   Count=1
Item = 2BBB   Count=1
Item = 3CCC   Count=1

If I want to add just 3 of just one item, I should use this: 如果我只想添加一个项目中的3个,我应该使用这个:

http://www.test.ccom/process?item=1AAA&item=1AAA&item=1AAA HTTP://www.test.ccom/process项目= 1AAA及项目= 1AAA及项目= 1AAA

And result page will be like this: 结果页面将如下所示:

Item = 1AAA   Count=3

My problem is that I can't send my request using GET method (because we want to add more than 100 per item and it will cause "Request-URI Too Large" error) 我的问题是我无法使用GET方法发送请求(因为我们要为每个项目添加超过100个,这将导致“Request-URI Too Large”错误)

I used two different methods to send this request by POST, but without success. 我使用两种不同的方法通过POST发送此请求,但没有成功。

First I used this function: 首先我使用了这个功能:

function post_by_form(path, params) {

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", path);
    form.setAttribute("style", "display: none")

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", "item");
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
}

It works when I test it with different variable names (name="item"+key), but it doesn't work when I use one variable name for all the inputs. 当我使用不同的变量名称(name =“item”+ key)测试它时它可以工作,但是当我为所有输入使用一个变量名时它不起作用。

Then I used this function to send the POST request by ajax: 然后我使用此函数通过ajax发送POST请求:

function post_by_ajax(path, params_arr){
    var http = new XMLHttpRequest();
    var url = path;

    var params = "";
    for(var key in params_arr) {
        if (params != "")
            params += "&item="+params_arr[key];
        else
            params += "item="+params_arr[key];
    }

    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(params);
}

The same result, both of this methods will return just one quantity for just one item (last one)... While we can submit a form with many input fields all with the same name, why I can not do it using these methods?! 同样的结果,这两种方法只返回一个项目的一个数量(最后一个)...虽然我们可以提交一个表格,其中包含许多具有相同名称的输入字段,但为什么我不能使用这些方法呢? ! Do I have any problem in the logic?! 我的逻辑有什么问题吗?! can somebody please help me?! 有人能帮帮我吗?!

If you're passing all variables with the same name ie: 'item' then the request handler has no way to differentiate between them. 如果您传递所有具有相同名称的变量,即:'item',则请求处理程序无法区分它们。 That is why you're getting only 1 element. 这就是为什么你只得到1个元素。 Try renaming the elements to item1=value&item2=value&item3=value. 尝试将元素重命名为item1 = value&item2 = value&item3 = value。

If you're passing 100 elements then you should definitely be using the post method. 如果你传递100个元素,那么你肯定应该使用post方法。 The name problem will exist for both post and get so make sure that all the items are named differently. post和get都会存在名称问题,因此请确保所有项目的名称都不同。

Using Firefox with the TamperData plugin, and a form that simply has four fields all specified as <input type="text" name="item"> I can see that the POST data does indeed send four variables all named "item" but with different values for each. 使用带有TamperData插件的Firefox,以及一个只有四个字段的表单,全部指定为<input type="text" name="item">我可以看到POST数据确实发送了四个全部命名为“item”的变量但是每个人的价值不同。

It is then up to the receiving server to do something sensible with that. 然后由接收服务器做一些合理的事情。 Most systems will just use one of the four values sent (maybe the first one, or the last one) but if there's already a server that correctly handles http://www.test.ccom/process?item=1AAA&item=1AAA&item=1AAA then your adding multiple fields all named "item" should work. 大多数系统将只使用发送四个值(也许是第一位的,或者最后一个)中的一个 ,但如果已经有一个能够正确处理服务器http://www.test.ccom/process?item=1AAA&item=1AAA&item=1AAA然后你添加所有名为“item”的多个字段应该有效。

If that's not the case, then you need to write something to handle that on the server end of things - no amount of javascript coding will do it. 如果情况并非如此,那么你需要在服务器端编写一些东西来处理它 - 没有多少javascript编码会这样做。 That would involve getting the whole POST body and processing it yourself, as most server-side frameworks (like I said) will generally just use one of the values. 这将涉及获取整个POST主体并自己处理它,因为大多数服务器端框架(如我所说)通常只使用其中一个值。

You can use TamperData or something similar to capture the HTTP data stream and see what is actually transmitted from the javascript you have now. 您可以使用TamperData或类似的东西来捕获HTTP数据流,并查看从您现在拥有的javascript实际传输的内容。


So ultimately, the answer to your question "Is it possible sending one variable multiple time via POST using javascript?" 所以最终,你的问题的答案“是否可以使用javascript通过POST多次发送一个变量?” is yes , it is possible. 是的 ,这是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM