AJAX是Asynchronous JavaScript And XML的首字母缩写.它提供了异步与服务器通信的能力.
要简单地解释一下,您可以向服务器发送请求并继续与用户进行用户交互.您无需等待服务器的响应.响应到达后,UI中的指定区域将自行更新并反映响应信息.整页不需要重新加载.
因此,您无法直接访问Java类作为URL来发出Ajax请求.它应该是任何映射的URL,如JSP,Servlets,PHP等.
创建JSP(例如hello.jsp)
String strResponse;
mail.Main objMain = new mail.Main();
strResponse = objMain.execute();
%>
在Ajax请求中
url: "hello.jsp",
编辑:添加示例:
$(function(){
function getData() {
var dataToBeSent = {
uName : $("#userName").val() , //
passwd: $("#password").val()
}; // you can change parameter name
$.ajax({
url : 'getDataServlet', // Your Servlet mapping or JSP(not suggested)
data :dataToBeSent,
type : 'POST',
dataType : 'html', // Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
success : function(response) {
$('#outputDiv').html(response); // create an empty div in your page with some id
},
error : function(request, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
});
在Servlet / JSP中访问你的参数request.getParameter(“uName”);