什么是跨域?如何解决跨域问题?

时间:2022-08-29 14:33:13

什么是跨域?

浏览器访问资源时,域名、端口、协议任一不同,都是跨域
域名:
主域名不同 http://www.baidu.com/index.html –>http://www.sina.com/test.js
子域名不同 http://www.666.baidu.com/index.html –>http://www.555.baidu.com/test.js
域名和域名ip http://www.baidu.com/index.html –>http://180.149.132.47/test.js
端口:
http://www.baidu.com:8080/index.html–> http://www.baidu.com:8081/test.js
协议:
http://www.baidu.com:8080/index.html–> https://www.baidu.com:8080/test.js
备注:
1、端口和协议的不同,只能通过后台来解决
2、localhost和127.0.0.1虽然都指向本机,但也属于跨域

如何解决跨域问题?

1、 通过jsonp跨域
JSONP(JSON with Padding:填充式JSON),应用JSON的一种新方法,可以让网页从别的网域要资料
eg:

 callback({"name","trigkit4"});

备注:
1、JSONP 只支持get请求、不支持post请求