詳解SpringBoot多跨域請(qǐng)求的支持(JSONP)
在我們做項(xiàng)目的過程中,有可能會(huì)遇到跨域請(qǐng)求,所以需要我們自己組裝支持跨域請(qǐng)求的JSONP數(shù)據(jù),而在4.1版本以后的SpringMVC中,為我們提供了一個(gè)AbstractJsonpResponseBodyAdvice的類用來支持jsonp的數(shù)據(jù)(SpringBoot接收解析web請(qǐng)求是依賴于SpringMVC實(shí)現(xiàn)的)。下面我們就看一下怎么用AbstractJsonpResponseBodyAdvice來支持跨域請(qǐng)求。
使用AbstractJsonpResponseBodyAdvice來支持跨域請(qǐng)求很簡單,只需要繼承這個(gè)類就可以了。具體代碼如下:
package com.zkn.learnspringboot.config; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice; /** * Created by wb-zhangkenan on 2016/12/1. */ @ControllerAdvice(basePackages = "com.zkn.learnspringboot.web.controller") public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice{ public JsonpAdvice() { super("callback","jsonp"); } }
下面我們寫個(gè)類來測試一下:
package com.zkn.learnspringboot.web.controller; import com.zkn.learnspringboot.domain.PersonDomain; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by wb-zhangkenan on 2016/12/1. */ @RestController @RequestMapping("/jsonp") public class JsonpTestController { @Autowired private PersonDomain personDomain; @RequestMapping(value = "/testJsonp",produces = MediaType.APPLICATION_JSON_VALUE) public PersonDomain testJsonp(){ return personDomain; } }
當(dāng)我們發(fā)送請(qǐng)求為:http://localhost:8003/jsonp/testJsonp的時(shí)候,結(jié)果如下:
當(dāng)我們發(fā)送的請(qǐng)求為:http://localhost:8003/jsonp/testJsonp?callback=callback的時(shí)候,結(jié)果如下所示:
看到區(qū)別了嗎?當(dāng)我們?cè)谡?qǐng)求參數(shù)中添加callback參數(shù)的時(shí)候,返回的數(shù)據(jù)就是jsonp的,當(dāng)我們請(qǐng)求參數(shù)中不帶callback的時(shí)候,返回的數(shù)據(jù)是json的。可以讓我們方便的靈活運(yùn)用。下面再奉上一個(gè)jsonp的完整案例。
前臺(tái)頁面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <script src="resources/js/jquery-2.1.4.min.js" type="text/javascript"></script> </head> <body> <input type="button" value="測試jsonp請(qǐng)求" onclick="testJsonp()" /> <script type="text/javascript"> function testJsonp() { $.ajax({ type:'get', url:'http://localhost:8003/jsonp/testJsonp', dataType:'jsonp', jsonp:"callback", success:function (data) { alert(data.userName+" "+data.passWord); }, error:function (err) { alert('出現(xiàn)錯(cuò)誤了!!!'); } }); } </script> </body> </html>
后臺(tái)代碼1:
package com.zkn.learnspringmvc.news.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by zkn on 2016/12/3. */ @Controller public class JsonpTestController { @RequestMapping("testJsonp") public String testJsonp(){ return "jsonp"; } }
下面我們發(fā)送請(qǐng)求如下:http://localhost:8080/LearnSpringMvc/testJsonp
當(dāng)我們點(diǎn)擊測試jsopn請(qǐng)求這個(gè)按鈕的時(shí)候,效果如下:
我們成功的實(shí)現(xiàn)了一個(gè)跨越的請(qǐng)求。更詳細(xì)的請(qǐng)求信息如下:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis實(shí)現(xiàn)查詢相冊(cè)數(shù)據(jù)列表流程講解
這篇文章主要介紹了Mybatis實(shí)現(xiàn)查詢相冊(cè)數(shù)據(jù)列表流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12Java?Http請(qǐng)求方式之RestTemplate常用方法詳解
這篇文章主要為大家介紹了Java?Http請(qǐng)求方式之RestTemplate常用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09使用Spring?AOP實(shí)現(xiàn)用戶操作日志功能
這篇文章主要介紹了使用Spring?AOP實(shí)現(xiàn)了用戶操作日志功能,功能實(shí)現(xiàn)需要一張記錄日志的log表,結(jié)合示例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05springboot項(xiàng)目中的bootstrap.yml配置不生效的原因及解決(沒有自動(dòng)提示)
新創(chuàng)建一個(gè) springboot項(xiàng)目,添加了 bootstrap.yml 文件,發(fā)現(xiàn)文件并沒有如預(yù)期變成綠色葉子,編寫的時(shí)候也沒有自動(dòng)提示,啟動(dòng)的時(shí)候,發(fā)現(xiàn)端口是8080,由此發(fā)現(xiàn)配置并沒有生效,所以本文給大家講解了springboot項(xiàng)目中的bootstrap.yml配置不生效的原因及解決2024-01-01SpringBoot+VUE實(shí)現(xiàn)數(shù)據(jù)表格的實(shí)戰(zhàn)
本文將使用VUE+SpringBoot+MybatisPlus,以前后端分離的形式來實(shí)現(xiàn)數(shù)據(jù)表格在前端的渲染,具有一定的參考價(jià)值,感興趣的可以了解一下2021-08-08Intellij IDEA遠(yuǎn)程debug教程實(shí)戰(zhàn)和要點(diǎn)總結(jié)(推薦)
這篇文章主要介紹了Intellij IDEA遠(yuǎn)程debug教程實(shí)戰(zhàn)和要點(diǎn)總結(jié)(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03SpringCloud_Sleuth分布式鏈路請(qǐng)求跟蹤的示例代碼
Spring Cloud Sleuth是一款針對(duì)Spring Cloud的分布式跟蹤工具,本文通過實(shí)例代碼介紹了SpringCloud_Sleuth分布式鏈路請(qǐng)求跟蹤,感興趣的朋友跟隨小編一起看看吧2023-02-02基于JavaMail實(shí)現(xiàn)簡單郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了基于JavaMail實(shí)現(xiàn)簡單郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08