伺服器端解決跨域問題的三種方法

2021-07-10 05:31:28 字數 1442 閱讀 1160

跨域是指html檔案所在的伺服器與ajax請求的伺服器是不同的ip+port,例如:

- 『192.168.1.1:8080』 與 『192.168.1.2:8080』是不同的域。

- 『192.168.1.1:8080』 與 『192.168.1.1:8081』是不同的域。

如果您使用的mvc框架是spring4.2以上的話,乙個@crossorigin就可以搞定。將@crossorigin加到controller上,那麼這個controller所有的請求都是支援跨域的,**如下:

@controller

@crossorigin

public

class

greetingcontroller

};}

< filter >

< filter-name > cors filter-name >

< filter-class > com.thetransactioncompany.cors.corsfilter filter-class >

< init-param >

< param-name > cors.alloworigin param-name >

< param-value > * param-value >

init-param >

< init-param >

< param-name > cors.supportedmethods param-name >

< param-value > get, post, head, put, delete param-value >

init-param >

< init-param >

< param-name > cors.supportedheaders param-name >

< param-value > accept, origin, x-requested-with, content-type, last-modified param-value >

init-param >

< init-param >

< param-name > cors.exposedheaders param-name >

< param-value > set-cookie param-value >

init-param >

< init-param >

< param-name > cors.supportscredentials param-name >

< param-value > true param-value >

init-param >

filter >

< filter-name > cors filter-name >

< url-pattern > /* url-pattern >

伺服器端解決跨域問題的三種方法

跨域是指html檔案所在的伺服器與ajax請求的伺服器是不同的ip port,例如 192.168.1.1 8080 與 192.168.1.2 8080 是不同的域。192.168.1.1 8080 與 192.168.1.1 8081 是不同的域。如果您使用的mvc框架是spring4.2以上的...

伺服器端解決跨域問題的三種方法

跨域是指html檔案所在的伺服器與ajax請求的伺服器是不同的ip port,例如 192.168.1.1 8080 與 192.168.1.2 8080 是不同的域。192.168.1.1 8080 與 192.168.1.1 8081 是不同的域。如果您使用的mvc框架是spring4.2以上的...

伺服器端解決跨域問題的三種方法

第一種 如果您使用的mvc框架是spring4.2以上的話,乙個 crossorigin就可以搞定。將 crossorigin加到controller上,那麼這個controller所有的請求都是支援跨域的,如下 controller crossorigin public class greetin...