注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

花随梦影蝶纷飞

记住该记住的,忘记该忘记的,改变能改变的,接受不能改变的。

 
 
 

日志

 
 

JavaScript跨域访问  

2014-05-13 18:29:16|  分类: ★网站知识★ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
ajax.js

//同步 GET AJAX
function AjaxSynGet(url) {
var xmlHttp;
//FrieFox
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
//IE
} else {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
var symbol = "?";
if (url.indexOf("?") >= 0) {
symbol = "&";
}
xmlHttp.open("GET", url + symbol + "rnd=" + Math.random(), false);
xmlHttp.send(null);
var text = xmlHttp.responseText;
return text;
}
//同步 POST AJAX
function AjaxSynPost(url, param) {
var xmlHttp;
//FrieFox
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
//IE
} else {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
var symbol = "?";
if (url.indexOf("?") >= 0) {
symbol = "&";
}
xmlHttp.open("POST", url + symbol + "rnd=" + Math.random(), false);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlHttp.send(param);
var text = xmlHttp.responseText;
return text;
}
//异步 GET AJAX
function AjaxGet(url, regFun) {
var xmlHttp;
//FrieFox
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
//IE
} else {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
if (xmlHttp != null) {
var symbol = "?";
if (url.indexOf("?") >= 0) {
symbol = "&";
}
//get
xmlHttp.open("GET", url + symbol + "rnd=" + Math.random(), true);
xmlHttp.send(null);
xmlHttp.onreadystatechange = function () {
//4 = "loaded"
if (xmlHttp.readyState == 4) {
//200 = OK
if (xmlHttp.status == 200) {
if(typeof(regFun)!='undefined'){
var text = xmlHttp.responseText;
regFun(text);
}
}
}
};
}
}
//异步 POST AJAX
function AjaxPost(url, param, regFun) {
var xmlHttp;
//FrieFox
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
//IE
} else {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
if (xmlHttp != null) {
var symbol = "?";
if (url.indexOf("?") >= 0) {
symbol = "&";
}
if(param=='' || typeof(param)=='undefined'){
param = null;
}
//get
xmlHttp.open("POST", url + symbol + "rnd=" + Math.random(), true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlHttp.send(param);
xmlHttp.onreadystatechange = function () {
//4 = "loaded"
if (xmlHttp.readyState == 4) {
//200 = OK
if (xmlHttp.status == 200) {
if(typeof(regFun)!='undefined'){
var text = xmlHttp.responseText;
regFun(text);
}
}
}
};
}
}
//跨域GET访问
function CrossDomainGet(url, regFun) {
var symbol = "?";
if (url.indexOf("?") >= 0) {
symbol = "&";
}
$.ajax({
type: "GET",
url: url + symbol + "rnd=" + Math.random(),
async:false,
dataType:"jsonp",
timeout: 10000,
jsonp: "jsonpCallback",
success: function (data) {
if(typeof(regFun)!='undefined'){
regFun(data);
}
},
error:function(){
if(typeof(regFun)!='undefined'){
regFun(data);
}
}
});
}
//跨域GET访问2
function CrossDomainGet2(url, regFun) {
var symbol = "?";
if (url.indexOf("?") >= 0) {
symbol = "&";
}
$.getJSON(url + symbol + "rnd=" + Math.random() + "&jsonpCallback=?",function(data){
if(typeof(regFun)!='undefined'){
regFun(data);
}
});
}

ajax.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>我的播放</title>
<script type="text/javascript" src="script/ajax.js"></script>
<script type="text/javascript" src="script/jquery-1.8.0.js"></script>
<script language="JavaScript">
function postInvoke(){
var url = "http://127.0.0.1:8080/myplayer/ajax?param=data";
var temp = CrossDomainGet(url,function(data){
document.getElementById("invokeValue").innerHTML = "返回信息:"+ data.result + " ";
});
}
function postInvoke2(){
var url = "http://127.0.0.1:8080/myplayer/ajax?param=data";
var temp = CrossDomainGet2(url,function(data){
document.getElementById("invokeValue2").innerHTML = "返回信息:"+ data.result + " ";
});
}
</script>
</head>

<body>
<span id="invokeValue"></span> </br>
<p>
<input type="submit" value="调用" onClick="postInvoke();" />
</p>
<span id="invokeValue2"></span> </br>
<p>
<input type="submit" value="调用二" onClick="postInvoke2();" />
</p>
</body>
</html>

AjaxServlet

package cn.domain;

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 AjaxServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 7643516584893678788L;

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("===============start================");
String host = request.getRemoteAddr();
System.out.println("host=" + host);
String uri = request.getRequestURI();
System.out.println("uri=" + uri);
String param = request.getParameter("param");
String rnd = request.getParameter("rnd");
String jsonpCallback = request.getParameter("jsonpCallback");
//============输出============
System.out.println("param=" + param + "&rnd=" + rnd + "&jsonpCallback=" + jsonpCallback);
StringBuilder json = new StringBuilder();
json.append("{");
json.append("\"result\":\"success\"");
json.append("}");
String result = jsonpCallback+"(" + json.toString() + ")";
response.getWriter().print(result);
response.getWriter().flush();
System.out.println("===============end================");
}

}

  评论这张
 
阅读(5)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017