function createXMLHttpRequest(){
var xmlhttp;
try{
xmlhttp = new XMLHttpRequest();//ie7及以上,其他浏览器
}catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");//ie6
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");//ie6以下
}catch(e){
throw "创建AJAX对象失败!";
}
}
}
return xmlhttp;
}
var xmlhttp = createXMLHttpRequest();
xmlhttp.open("GET","http://localhost:8080/SimpleBlog/AjaxTest",true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = function(result){
if(xmlhttp.readyState==4 && xmlhttp.status == 200){
alter(result.test);
}
};
function get() {
var url = 'http://localhost:8080/SimpleBlog/AjaxTest?callback=callback';
var script = document.createElement('script');
script.setAttribute("type","text/javascript");
script.src = url;
document.body.appendChild(script);
}
function callback(va){
alert(va.test);
}
boolean jsonP = false;
String cb = this.request.getParameter("callback");
if (cb != null) {
jsonP = true;
response.setContentType("text/javascript");
} else {
response.setContentType("application/x-json");
}
PrintWriter out = response.getWriter();
if (jsonP) {
try {
out.println(cb + "({\"test\":\"1\"})");
out.flush();
out.close();
} catch (Exception e) {
throw e;
}
}
$(document).ready(function (){
$('#jqueryajax').bind('click', function(){
$.ajax({
type: 'get',
async: false,
url: 'http://localhost:8080/SimpleBlog/AjaxTest1',
dataType: 'jsonp',
jsonp: 'callback',
success: function(json){
alert(json.result);
},
error: function(){
alert('fail');
}
});
});
});
public class AjaxTest1 extends ActionSupport {
private String result;
public String getResult() {
return result;
}
public String execute() {
this.result = "1";
return "jqueryajax";
}
}
<action name="AjaxTest1" class="AjaxTest1"> <result name="jqueryajax" type="json"> <param name="callbackParameter">callback</param> </result> </action>
function createCORSRequest(method,url){
var xhr=new XMLHttpRequest();
if('withCredentials' in xhr){
xhr.open(method,url,true);
}else if(typeof XDomainRequest!='undefined'){
xhr=new XDomainRequest();
xhr.open(method,url);
}else{
xhr=null;
}
return xhr;
}
function xhr2(){
var request=createCORSRequest('GET','http://localhost:8080/SimpleBlog/AjaxTest1');
if(request){
request.onload=function(){
alert(request.responseText);
}
request.onerror=function(e){
alert('error');
}
request.send();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有