我有一个来自javascript文件的ajax调用,我想要将想要删除的用户的id作为参数传递:
function eliminaUtente(id,nome){
if (confirm("Sei sicuro di voler eliminare l'utente "
+ nome
+ "?")) {
var xmlHttp2 = new XMLHttpRequest();
xmlHttp2.open("POST", "EliminaUtente", true);
xmlHttp2.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
var params2 = "id=" + id;
xmlHttp2.send(params2);
xmlHttp2.onreadystatechange = function() {
if (xmlHttp2.readyState == 4)
{
alert(xmlHttp2.status); <-----------this prints always 0!
if (xmlHttp2.status == 200) //
{
alert("utente eliminato!");
} else {
alert("An error occurred while communicating with server.");
}
}
};}}
在对应的名为EliminaUtente的Servlet中,我有以下代码:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String id = request.getParameter("id");
System.out.println(id);
String query = "delete from utente where idutente=" + id;
System.out.println(query);
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager
.getConnection("jdbc:mysql://localhost/Spinning?user=root");
PreparedStatement prest = con.prepareStatement(query);
prest.executeUpdate();
response.setContentType("text/plain");
PrintWriter ajaxWriter = response.getWriter();
ajaxWriter.print("ok");
ajaxWriter.flush();
ajaxWriter.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
response.setContentType("text/plain");
PrintWriter ajaxWriter = response.getWriter();
ajaxWriter.print("ko");
ajaxWriter.flush();
ajaxWriter.close();
}
}}
我不明白你帮我的problem...can在哪里?;)
发布于 2013-02-10 22:59:43
我尝试了你的代码,并做了一些修改,我想解释一下我做了什么,以及我从中学到了什么。我读了一些源代码。首先,我读取了XMLHttpRequest对象和onreadyState事件。我实现了您的示例,包括PUT和GET操作方法。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>com.test.testServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
</web-app>testServlet.java
package com.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class testServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
/*super.doPost(req, resp);*/
String strId = req.getParameter("id");
System.out.println(strId);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(req, resp);
String strId = req.getParameter("id");
System.out.println(strId);
}
}和主要部分NewFile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<input type="button" value="Submit" onclick="eliminaUtente('1')" width="100%" />
</body>
<script language="javascript" type="text/javascript">
function eliminaUtente(id) {
var xmlHttp = new XMLHttpRequest();
var url = "test/NewFile.jsp?id=" + id;
xmlHttp.open("POST", url, true);
xmlHttp.send(url);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert("utente eliminato!");
} else {
alert("An error occurred while communicating with server.");
}
};
}
</script>
</html>通过这种方式,我编写了参数1(我在jsp文件方法调用中对其进行了硬编码)并将其写入console,这里的第一件事就是删除xmlHttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded");,因为如果方法类型是POST,则默认加密是这样的。
enctype = content-type [CI]
This attribute specifies the content type used to submit the form to the server (when the value of method is "post"). The default value for this attribute is "application/x-www-form-urlencoded". The value "multipart/form-data" should be used in combination with the INPUT element, type="file".POST来说,不需要加密default是可以的。异步,这是开放方法签名,这里是参数,意思是如果它是假的,不要等待来自服务器的响应,实现另一行,当响应到来时,它会运行。如果是真的,请等待响应到来。实际上,我尝试了他们的机器人,它是有效的。
GET试了一下。如果您希望将其与GET一起使用,则应该添加用于加密的xmlHttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded");代码,并从send()方法中删除url参数。函数eliminaUtente(id) {
var xmlHttp =新的XMLHttpRequest();var url = "test/NewFile.jsp?id=“+ id;xmlHttp.open("GET",url,true);xmlHttp2.setRequestHeader(”内容类型“,”应用程序/x-www-form-urlencoded“);xmlHttp.send();xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {alert(”完全消除!“);} else {alert(“与服务器通信时出错。”);} };}
注意:我在火狐中尝试了这段代码,我创建了xmlHttpRequest对象。对于所有浏览器(包括IE6),请确保使用:
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}发布于 2013-02-04 03:06:32
打字错误?
alert(xmlHttp2.status); <-打印始终为0!现在有“2”
发布于 2013-02-08 19:26:34
问题是我必须在这一行中放入false:
xmlHttp2.open("POST", "EliminaUtente", FALSE);https://stackoverflow.com/questions/14676066
复制相似问题