springMVC实现jsonp的跨域请求
生活随笔
收集整理的這篇文章主要介紹了
springMVC实现jsonp的跨域请求
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.在Controller層實(shí)現(xiàn)jsonp接口
/***Project Name: gspReceiveServer*File Name: EquipmentController.java*Package Name: com.ltmap.platform.equipment.controller*Date: 2016年8月3日 上午9:46:35*Copyright (c) 2016,578888218@qq.com All Rights Reserved. */package com.ltmap.platform.equipment.controller;import java.io.IOException;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody;import com.ltmap.platform.equipment.service.EquipmentService;/***Title: EquipmentController<br/>*Description:*@Company: 青島勵(lì)圖高科<br/>*@author: 劉云生*@version: v1.0*@since: JDK 1.7.0_80*@Date: 2016年8月3日 上午9:46:35 <br/> */ @Controller public class EquipmentController {@AutowiredEquipmentService equipmentService;@RequestMapping(value = "updateEquipment", method ={RequestMethod.POST,RequestMethod.GET}, produces = "application/json; charset=utf-8")@ResponseBodypublic int updateEquipment(){return equipmentService.updateEquipment();}@RequestMapping("updateEquipmentJsonp") public void get(HttpServletRequest request,HttpServletResponse response) { int flag=equipmentService.updateEquipment();response.setContentType("text/plain"); String callbackFunName =request.getParameter("<span style="color:#ff6666;">callbackparam</span>");//得到j(luò)s函數(shù)名稱 try { response.getWriter().write(callbackFunName + "([ { flag:\""+flag+"\"}])"); //返回jsonp數(shù)據(jù) } catch (IOException e) { e.printStackTrace(); } } }2.利用ajax進(jìn)行jsonp的跨域請(qǐng)求
3.注意:紅字參數(shù)名稱要保持一致
總結(jié)
以上是生活随笔為你收集整理的springMVC实现jsonp的跨域请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 爬虫实现自动登陆抽屉网,实现对文章点赞,
- 下一篇: 网页title图标设置