javascript前端:
function callback(data) {
alert(data);
}
function addScriptTag(src) {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.src = src;
document.body.appendChild(script);
}
window.onload = function() {
addScriptTag('http://www.xxx.com/test/front/json/cross-domain!jsonStrInfo.shtml?callback=callback');
}
Jquery前端:
<script>
/*
function callback(data) {
alert(data);
}
*/
$.ajax({
url:'http://www.xxx.com/test/front/json/cross-domain!jsonStrInfo.shtml?callback=?',
dataType:"jsonp",
success:function(data){
$("#myDiv").html(data);
}
});
</script>
后台Action:
public String jsonStrInfo() {
try {
String path = CrossDomainAction.class.getClassLoader().getResource("oscache.properties").toString();
System.out.println(path);
path = path.substring(6);
InputStream is = new FileInputStream(path);
Properties p = new Properties();
p.load(is);
System.out.println(System.getProperty("os.name"));
System.out.println(p.getProperty("cache.capacity"));
count(path);
HttpServletResponse response = this.getResponse();
HttpServletRequest request = this.getRequest();
String callback = request.getParameter("callback");
String returnResult = "";
if(callback != null && !"".equals(callback)){
returnResult = callback + "(\"这是jsonp跨域测试返回的数据\")";
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/javascript");
System.out.println("dddd");
PrintWriter writer = response.getWriter();
writer.println(returnResult);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}