检索从ajax req发送的数据

时间:2011-03-10 05:48:32

标签: java ajax servlets struts

我需要在ajax post请求中向服务器发送大量字符串。如果我将其添加到网址的末尾,我可以在服务器中使用request.getParameter()方法。

但是我无法在url中附加大字符串。所以我想使用XMLHttpRequest的send()方法发送,而不是将其附加到url。

但我无法使用server.getParameter()在服务器中检索相同内容。

如何在j2ee服务器中检索从ajax请求发送的数据? 请指导我。

2 个答案:

答案 0 :(得分:1)

如果您有一个ajax密集型webapp,我建议使用javascript库(如jquery)来处理ajax。在jQuery中,你可以这样做:

$.post("your_url.php", { param1: "value1", param2: "value2" } );

答案 1 :(得分:0)

无论您是否使用AJAX,都会更改browser to browser更改的网址的实际长度。

POST所有数据都会更好。下面是使用AJAX执行相同操作的代码:

var http = new XMLHttpRequest(); // Get the correct http object depending on browser
var url = "YOUR_URL.php";
var params = "param1=value1&param2=value2";
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);

希望这有帮助。