ajax未调用到后台的action
页面源码没错,action的URL也没问题,直接在浏览器中调用该acion可正确执行。
但用ajax却无法调用,请大侠指导啊!!谢谢!!
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script type="text/javascript">
function queryCertificateID(){
alert("开始查询证件类型!");
$.ajax({
type: "GET",
url: "<%=basePath%>/basebiz/queryCertificateTypeAction!queryCertificateType",
success:function(data){
alert("查询证件类型成功!");
},
error: function(){
alert("查询证件类型失败!");
}
});
}
</script>
<body onload="queryCertificateID()">
<form id="form1" name="form1" method="post" action="">
<table width="300" border="0" align="center">
<tr>
<td width="114">手机号码</td>
<td width="176"><label>
<input type="text" name="user.userPhone" />
</label></td>
</tr>
<tr>
<td>电子邮箱</td>
<td><input type="text" name="user.userMail" /></td>
</tr>
<tr>
<td>姓 名</td>
<td><input type="text" name="user.userName" /></td>
</tr>
<tr>
<td>证件类型</td>
<td><label>
<select name="user.certificateID">
<s:iterator value="#request.CertificateType">
<option value="<s:property value="#request.CertificateType.certificateID"/>"> <s:property value="#request.CertificateType.certificateName"/>
</option>
</s:iterator>
</select>
</label></td>
</tr>
<tr>
<td>证件号码</td>
<td><input type="text" name="user.certificateCode" /></td>
</tr>
<tr>
<td>密 码</td>
<td><input type="password" name="user.userPasswrod" /></td>
</tr>
<tr>
<td>确认密码</td>
<td><input type="password" name="confirmPassword" /></td>
</tr>
<tr>
<td>优 惠 码</td>
<td><input type="text" name="promocode" /></td>
</tr>
<tr>
<td>验 证 码</td>
<td><input type="text" name="verificationCode" /></td>
</tr>
<tr>
<td><label>
<div align="right">
<input type="submit" name="Submit" value="注册" />
</div>
</label></td>
<td><div align="left">同意合约
<label>
<input type="checkbox" name="accept" value="1" />
</label>
</div></td>
</tr>
</table>
</form>
</body>
</html>
ajax?struts2 spring ssh
[解决办法]
你$.ajax({
这个是需要jquery.js的,
没引用jquery.js
怎么调用
[解决办法]
按这个格式来试试:
$.ajax({
url:"",
type:"POST",
dataType:"json",
data:"",
success:function(data) {
}
});
[解决办法]
不用jquery的,你就用这段代码试试:
/**
* 操作ajax的对象
*/
var ajax = {
/**
* @see 创建ajax核心对象,兼容浏览器有:IE6,7,8,9,10,谷歌,火狐,欧朋,360极速,360安全,苹果,搜狗,遨游,猎豹,腾讯
* @return XMLHttpRequest
*/
getCore : function() {
var xmlHttp = null;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
if (xmlHttp.overrideMimeType) {
xmlHttp.overrideMimeType("text/xml");
}
} else {
if (window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
}
if (xmlHttp == null) {
dv.alert("\u8bf7\u4f7f\u7528IE\u6d4f\u89c8\u5668!");
}
return xmlHttp;
},
remoteBack : null,// 跨域ajax的回调事件
core : null,// ajax的核心对象
/**
* @see 处理ajax参数
* @param param 参数, 可以是js对象或字符串: id=1&name=lisi
* @return String
*/
getParam : function(param) {
var randomStr = "ajaxParamRandom=" + Math.random();
if (param == null) {
return randomStr;
}
var info = "";
if (typeof(param) == "object") {
for (var key in param) {
info += key + "=" + param[key] + "&";
}
info = (info.length > 0 ? info.substring(0, info.length - 1) : info);
} else {
info = param;
}
return info + "&" + randomStr;
},
/**
* @see ajax的回调函数
* @param callback 用户自定义回调函数
* @param url 请求的url
*/
doCallback : function(callback, url) {
if (ajax.core.readyState == 4) {
if (ajax.core.status == 200) {
ajax.doBack(callback, new String(ajax.core.responseText));
} else if (ajax.core.status == 0
[解决办法]
ajax.core.status == 12029) {
//window.location.href = path + "/login.htm";
dv.alert("请求超时, 请稍候重试");
} else if (ajax.core.status == 404) {
dv.showGress("0", "1");
dv.alert("找不到资源: " + url);
}
}
},
/**
* @see 处理ajax的回调函数
* @param callback 用户自定义回调函数
* @param result 请求的结果
*/
doBack : function(callback, result) {
if (callback == null) {
return;
}
if (null != result && result != "") {
if (result == "null") {
callback(null);
} else {
var backObject = null;
if (result == "true"
[解决办法]
result == "false") {
backObject = eval(result);
} else if (!isNaN(result)) {
backObject = parseFloat(result);
} else if ((result.substring(0, 1) == "[" && result.substring(result.length - 1, result.length) == "]")
[解决办法]
(result.substring(0, 1) == "{" && result.substring(result.length - 1, result.length) == "}")) {
backObject = eval("(" + result + ")");
} else {
backObject = result;
}
callback(backObject);
}
} else {
callback(result);
}
},
/**
* @see ajax的post请求
* @param url 请求的url
* @param param 参数列表,可以是字符串或js对象或null
* @param callback 用户自定义回调函数
*/
post : function(url, param, callback) {
ajax.doPost(url, param, true, callback);
},
/**
* @see ajax的post请求
* @param url 请求的url
* @param param 参数列表,可以是字符串或js对象或null
* @param isAsynch 是否异步
* @param callback 用户自定义回调函数
*/
doPost : function(url, param, isAsynch, callback) {
ajax.core = ajax.getCore();
if (ajax.core != null) {
ajax.core.onreadystatechange = function() {
ajax.doCallback(callback, url);
};
ajax.core.open("POST", url, isAsynch == null ? false : isAsynch);
ajax.core.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.core.send(ajax.getParam(param));
}
},
/**
* @see ajax的get请求
* @param url 请求的url
* @param callback 用户自定义回调函数
*/
get : function(url, callback) {
ajax.doGet(url, true, callback);
},
/**
* @see ajax的get请求
* @param url 请求的url
* @param isAsynch 是否异步
* @param callback 用户自定义回调函数
*/
doGet : function(url, isAsynch, callback) {
ajax.core = ajax.getCore();
if (ajax.core != null) {
url += (url.indexOf("?") != -1 ? "&" : "?") + "ajaxParamRandom=" + Math.random();
ajax.core.onreadystatechange = function() {
ajax.doCallback(callback, url);
};
ajax.core.open("GET", url, isAsynch == null ? false : isAsynch);
ajax.core.send(null);
}
},
/**
* @see 跨域ajax(只支持get请求, 并且主域名必须一致)
* @param url 请求的url
* @param callback 用户自定义回调函数
*/
remote : function(url, callback) {
ajax.remoteBack = callback;
url += (url.indexOf("?") != -1 ? "&" : "?") + "ajaxParamRandom=" + Math.random();
if (com.getO("frmRemoteAjax") != null) {
document.body.removeChild(com.getO("frmRemoteAjax"));
}
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "frmRemoteAjax");
iframe.style["display"] = "none";
document.body.appendChild(iframe);
com.getO("frmRemoteAjax").src = url;
},
/**
* 跨域ajax的请求的回调事件
* @param result 请求的结果
*/
remoteCallback : function(result) {
document.body.removeChild(com.getO("frmRemoteAjax"));
ajax.doBack(ajax.remoteBack, result);
}
};
ajax.post("你的url", {param : "你的参数"}, function(json) {
alert(json.message);
});