jquery ajax 跨域请求
jsp:
$.ajax({ url: "http://10.143.132.116:8080/plugin-test/checkUser.action?userId="+userId+"&rand="+Math.random(), dataType: "jsonp", jsonpCallback: "callback", crossDomain: true, type: 'post', async :false, success: function(data){ if(data.flag=="0"){ alert("no auth"); }else{ alert("has auth"); } }, cache:false, error: function(XMLHttpRequest, textStatus, errorThrown) { if(XMLHttpRequest.status==500){ art.dialog.alert('服务器异常,请重试或者联系管理员', function () {}); }else if(XMLHttpRequest.status==404){ art.dialog.alert('请求路径找不到,请重试或者联系管理员', function () {}); }else if(XMLHttpRequest.status==0){ art.dialog.alert('服务器已停止,请重试或者联系管理员', function () {}); }else{ art.dialog.alert('请求异常,请重试或者联系管理员', function () {}); } }, });
dataType: "jsonp",
jsonpCallback: "callback",
crossDomain: true,
三个比较重要的参数
action:
JSONObject item = new JSONObject(); int flag = 0; if (gu != null) { //throw new Exception("该用户已与此角色关联,用户ID:" + userId); flag = 1;//1表示存在 item.put("flag", flag); }else{ flag = 0;//0表示不存在 item.put("flag", flag); } response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); out.print("callback" + "(" + item.toString() + ")"); out.flush(); out.close(); return null;