如何使用原生的XMLHttpRequest对象提交表单
答案:2 悬赏:0 手机版
解决时间 2021-11-26 06:56
- 提问者网友:喧嚣尘世
- 2021-11-26 02:58
如何使用原生的XMLHttpRequest对象提交表单
最佳答案
- 五星知识达人网友:思契十里
- 2021-11-26 03:22
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.state == 4) {
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
console.log(xhr.responseText);
} else {
alert("HttpRequest was unsccessful: " + xhr.status);
}
}
}
xhr.open("post", "form.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var form = document.getElementById("info");
xhr.send(serialize(form));
xhr.onreadystatechange = function(){
if(xhr.state == 4) {
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
console.log(xhr.responseText);
} else {
alert("HttpRequest was unsccessful: " + xhr.status);
}
}
}
xhr.open("post", "form.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var form = document.getElementById("info");
xhr.send(serialize(form));
全部回答
- 1楼网友:未来江山和你
- 2021-11-26 03:53
Servlet
public class AjaxServlet extends HttpServlet {private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.append("请求地址: ").append(request.getContextPath());
out.append("参数:userName=").append(request.getParameter("userName"));
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
web.xml
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯