博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jquery,ajax 跨域访问
阅读量:4118 次
发布时间:2019-05-25

本文共 2138 字,大约阅读时间需要 7 分钟。

 Jsonp(JSON with Padding)是资料格式 json 的一种“使用模式”,可以让网页从别的网域获取资料。

 

1.客户端

Html代码  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>Insert title here</title>  
  6. <script type="text/javascript" src="resource/js/jquery-1.7.2.js"></script>  
  7. </head>  
  8. <script type="text/javascript">  
  9. $(function(){     
  10.     /*  
  11.     //简写形式,效果相同  
  12.     $.getJSON("http://app.example.com/base/json.do?sid=1494&busiId=101&jsonpCallback=?",  
  13.             function(data){  
  14.                 $("#showcontent").text("Result:"+data.result)  
  15.     });  
  16.     */  
  17.     $.ajax({  
  18.         type : "get",  
  19.         async:false,  
  20.         url : "http://app.example.com/base/json.do?sid=1494&busiId=101",  
  21.         dataType : "jsonp",//数据类型为jsonp  
  22.         jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数  
  23.         success : function(data){  
  24.             $("#showcontent").text("Result:"+data.result)  
  25.         },  
  26.         error:function(){  
  27.             alert('fail');  
  28.         }  
  29.     });   
  30. });  
  31. </script>  
  32. <body>  
  33. <div id="showcontent">Result:</div>  
  34. </body>  
  35. </html>  
Insert title here
Result:

 二.服务器端

Java代码  
  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import net.sf.json.JSONObject;  
  8. import org.springframework.stereotype.Controller;  
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10.   
  11. @Controller  
  12. public class ExchangeJsonController {  
  13.     @RequestMapping("/base/json.do")  
  14.     public void exchangeJson(HttpServletRequest request,HttpServletResponse response) {  
  15.        try {  
  16.         response.setContentType("text/plain");  
  17.         response.setHeader("Pragma""No-cache");  
  18.         response.setHeader("Cache-Control""no-cache");  
  19.         response.setDateHeader("Expires"0);  
  20.         Map<String,String> map = new HashMap<String,String>();   
  21.         map.put("result""content");  
  22.         PrintWriter out = response.getWriter();       
  23.         JSONObject resultJSON = JSONObject.fromObject(map); //根据需要拼装json  
  24.         String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数  
  25.         out.println(jsonpCallback+"("+resultJSON.toString(1,1)+")");//返回jsonp格式数据  
  26.         out.flush();  
  27.         out.close();  
  28.       } catch (IOException e) {  
  29.        e.printStackTrace();  
  30.       }  
  31.     }  
  32. }  

转载地址:http://gjcpi.baihongyu.com/

你可能感兴趣的文章
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>
SQL1015N The database is in an inconsistent state. SQLSTATE=55025
查看>>
RQP-DEF-0177
查看>>
Linux查看mac地址
查看>>
Linux修改ip
查看>>
MySQL字段类型的选择与MySQL的查询效率
查看>>
Java的Properties配置文件用法【续】
查看>>
JAVA操作properties文件的代码实例
查看>>
IPS开发手记【一】
查看>>
Java通用字符处理类
查看>>
文件上传时生成“日期+随机数”式文件名前缀的Java代码
查看>>
Java代码检查工具Checkstyle常见输出结果
查看>>
北京十大情人分手圣地
查看>>
Android自动关机代码
查看>>
Android中启动其他Activity并返回结果
查看>>
2009年33所高校被暂停或被限制招生
查看>>
GlassFish 部署及应用入门
查看>>
iWatch报错: Authorization request cancled
查看>>
iWatch报错: Authorizationsession time out
查看>>