您的位置:首页 > Web前端 > JQuery

JQuery,ajax,jsonp,struts实现跨域

2014-10-30 16:53 447 查看
页面javaScript:

<script>
$(document).ready(
function() {
var SERVER_VISIT_PATH = 'http://localhost:8080/unemp';//另一个项目所在服务器路径,因为我是在本地测的所以用的是localhost

var actionPath="/sysListenerInfo.action";

$.ajax({
url:SERVER_VISIT_PATH+actionPath,
type:"get",
async:false,
dataType:"jsonp",
jsonp:"callbacks",//传递给请求处理程序或页面的,用以获得jsonp回调函数的参数名称(默认为:callback)

success:function(data){
var str = "现有人数"+data.currentCount+"个";
$("#currentCount").val(str);
$("#maxCount").html(data.maxCount);
$("#totalCount").html(data.totalCount);
$("#maxInactiveInterval").html(data.maxInactiveInterval);

$("#usedMemory").html(data.usedMemory);
$("#totalMemory").html(data.totalMemory);
$("#maxMemory").html(data.maxMemory);
$("#percentFree").html(data.percentFree);
$("#activeThreadCount").html(data.activeThreadCount);

},
error:function(v){
console.log(v);
alert("das");
}
});
});
</script>

struts配置如下:
  ajax调用不需要返回值

<action name="sysListenerInfo" class="cn.fulong.unemp.web.action.sysListener.datamonitorAction" method="sysListenerInfo">

</action>

后台action主要代码如下:

 private String callbacks;

public String getCallbacks() {

  return callbacks;

 }

 public void setCallbacks(String callbacks) {

  this.callbacks = callbacks;

 }

    public String sysListenerInfo(){

  MonitorBean monitorBean = new MonitorBean();

  System.out.println("currentCount"+monitorBean.getCurrentCount());

  currentCount=monitorBean.getCurrentCount();

  maxCount =monitorBean.getMaxCount();

  totalCount = monitorBean.getTotalCount();

  maxInactiveInterval = monitorBean.getMaxInactiveInterval();

  startupTime = monitorBean.getStartupTime();

  usedMemory = monitorBean.getUsedMemory();

  totalMemory = monitorBean.getTotalMemory();

  maxMemory = monitorBean.getMaxMemory();

  percentFree = monitorBean.getPercentFree();

  activeThreadCount = monitorBean.getActiveThreadCount();

  //List<UserAction> userlist = monitorBean.getUsers();

  System.out.println("userlist.size="+monitorBean.getUsers());

 //拼接json数据

  msg = callbacks+"({"+

  "\"currentCount\":"+"\""+currentCount+"\","+

  "\"maxCount\":"+"\""+maxCount+"\","+

  "\"totalCount\":"+"\""+totalCount+"\","+

  "\"maxInactiveInterval\":"+"\""+maxInactiveInterval+"\","+

  "\"usedMemory\":"+"\""+usedMemory+"\","+

  "\"totalMemory\":"+"\""+totalMemory+"\","+

  "\"maxMemory\":"+"\""+maxMemory+"\","+

  "\"percentFree\":"+"\""+percentFree+"\","+

  "\"activeThreadCount\":"+"\""+activeThreadCount+"\""

  +"})";

  try {

   ServletActionContext.getResponse().getWriter().print(msg);//把msg传到跨域调用该action的前台页面

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  return null;//返回值必须为null

 }

ajax请求参数说明:


dataType  String

预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如XML MIME类型就被识别为XML。在1.4中,JSON就会生成一个JavaScript对象,而script则会执行这个脚本。随后服务器端返回的数据会根据这个值解析后,传递给回调函数。可用值:

"xml": 返回 XML 文档,可用 jQuery 处理。

"html": 返回纯文本 HTML 信息;包含的script标签会在插入dom时执行。

"script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了"cache"参数。'''注意:'''在远程请求时(不在同一个域下),所有POST请求都将转为GET请求。(因为将使用DOM的script标签来加载)

"json": 返回 JSON 数据 。

"jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如
"myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。

"text": 返回纯文本字符串


jsonp   String

在一个jsonp请求中重写回调函数的名字。这个值用来替代在"callback=?"这种GET或POST请求中URL参数里的"callback"部分,比如{jsonp:'onJsonPLoad'}会导致将"onJsonPLoad=?"传给服务器。


jsonpCallback   String

为jsonp请求指定一个回调函数名。这个值将用来取代jQuery自动生成的随机函数名。这主要用来让jQuery生成度独特的函数名,这样管理请求更容易,也能方便地提供回调函数和错误处理。你也可以在想让浏览器缓存GET请求的时候,指定这个回调函数名。

   ajax jsonp与普通的ajax请求的主要区别在于——请求响应结果的处理。如上面代码所示的响应结果为:
success_jsonpCallback([ { name:"John"} ] ); ————其实就是,调用jsonp回调函数success_jsonpCallback,并将要响应的字符串或json传入此方法(作为参数值),其底层的实现,大概的猜想应该是:

function success_jsonpCallback(data)
        {
            success(data);
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ajax jquery js struts jsonp