web前端跨域访问

第一种写法
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>跨域测试</title>
	<META content="text/html; charset=UTF-8" http-equiv=content-type>
	<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#button").click(function(){
				$.getJSON("http://localhost:8080/crossDomainServer/test/toindex.do?name=zhangsan&jsoncallback=?");	
			});

		});
		function crossDomain(data){
			$("#kuayu").html(data.password);
		};
	</script>
  </head>
  
  <body>
   <center>张三的密码是<span id="kuayu"></span><hr>
   		<input id="button" type="button" value="点击进行跨域">
   </center>
  </body>
</html>

第二种写法

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>跨域测试</title>
	<META content="text/html; charset=UTF-8" http-equiv=content-type>
	<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#button").click(function(){
				var scriptObj = document.createElement("script");
				scriptObj.src="http://localhost:8080/crossDomainServer/test/toindex2.do?name=zhangsan";
				document.body.appendChild(scriptObj);
			});
			
		});
		function crossDomain(data){
			$("#kuayu").html(data.age);
		};
	</script>
  </head>
  
  <body>
   <center>李四的年龄多大了?<span id="kuayu"></span><hr>
   		<input id="button" type="button" value="点击进行跨域">
   </center>
  </body>
</html></span><span style="font-size:14px;">
</span>

第三种写法

<span style="font-size:14px;"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>跨域测试</title>
	<META content="text/html; charset=UTF-8" http-equiv=content-type>
	<script type="text/javascript" src="js/jquery-1.6.2.js"></script>
	<script type="text/javascript">
	$(function(){
		$("#button").click(function(){
			$.ajax({
				   async:false,
				   type: "get",
				   dataType: "jsonp", 
				   jsonp: "callbackparam",//服务端用于接收callback调用的function名的参数
				   jsonpCallback:"success_jsonpCallback",//callback的function名称
				   url: "http://localhost:8080/crossDomainServer/test/toindex3.do",
				   data: "name=John",
				   success: function (json) {//客户端jquery预先定义好的callback函数,成功获取跨域服务器上的json数据后,会动态执行这个callback函数
						$("#kuayu").html(json.hoppy);
				   },
		    	   error:function(){
		        		alert('fail');
		    	   }
			});
		});
	});
	</script>
  </head>
  
  <body>
   <center>John最喜欢吃什么?<span id="kuayu"></span><hr>
   		<input id="button" type="button" value="点击进行跨域">
   </center>
  </body>
</html></span><span style="font-size:14px;">
</span>

三种不同的写法后台访问的controller如下,此controller存在于另一个项目中

<span style="font-size:14px;">package com.liantuo;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping({"/test"})
public class CrossDomain
{
  @RequestMapping({"/toindex.do"})
  public void toindex(HttpServletRequest request, HttpServletResponse response)
    throws IOException
  {
    String name = request.getParameter("name");
    String callback = request.getParameter("jsoncallback");
    System.out.println(name + "===============" + callback);
    String s = "{\"password\":\"123456\"}";
    String ss = "crossDomain(" + s + ")";
    System.out.println(ss);
    response.getWriter().write(ss);
  }

  @RequestMapping({"/toindex2.do"})
  public void toindex2(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=UTF-8");
    String name = request.getParameter("name");
    String callback = request.getParameter("jsoncallback");
    System.out.println(name + "===============" + callback);
    String s = "{\"age\":\"年龄18\"}";
    String ss = "crossDomain(" + s + ")";
    System.out.println(ss);
    response.getWriter().write(ss);
  }
  @RequestMapping({"/toindex3.do"})
  public void toindex3(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=UTF-8");
    String name = request.getParameter("name");
    String callbackparam = request.getParameter("callbackparam");
    System.out.println(name + "===============" + callbackparam);
    String s = "{\"hoppy\":\"苹果\"}";
    String ss = callbackparam + "(" + s + ")";
    System.out.println(ss);
    response.getWriter().write(ss);
  }
}</span>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值