💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 什么是跨域? 跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是<span style="color:red;">浏览器施加的</span>安全限制。 >[info]所谓同源是指,域名,协议,端口均相同,不明白没关系,举个栗子: - http://a.yagnxigua.com/index.html 调用 http://a.yagnxigua.com/server.php (非跨域) - http://www.123.com/index.html 调用 http://www.456.com/server.php (主域名不同:123/456,跨域) - http://a.yagnxigua.com/index.html 调用 http://b.yagnxigua.com/server.php (子域名不同:a/b,跨域) - http://a.yagnxigua.com:8080/index.html 调用 http://a.yagnxigua.com:8081/server.php (端口不同:8080/8081,跨域) - http://a.yagnxigua.com/index.html 调用 https://a.yagnxigua.com/server.php (协议不同:http/https,跨域) >[warning]注意:localhost和127.0.0.1虽然都指向本机,但也属于跨域,同理域名和域名对应的ip也属于跨域。 浏览器执行javascript脚本时,会检查这个脚本属于哪个页面,如果不是同源页面,就不会被执行。 ## 解决办法: 1、JSONP: 使用方式就不赘述了,但是要注意JSONP只支持GET请求,不支持POST请求。 2、PHP端修改header(XHR2方式) 在php接口脚本中加入以下两句即可(也是放到b域名下): header('Access-Control-Allow-Origin:\*');//允许所有来源访问(这是允许谁来访问) header('Access-Control-Allow-Method:POST,GET');//允许访问的方式 3、修改nginx配置文件       add\_header Access-Control-Allow-Origin http://a.yangxigua.com;       add\_header Access-Control-Allow-Headers X-Custom-Header;       add\_header Access-Control-Allow-Methods GET,POST,OPTIONS; ![](https://box.kancloud.cn/5e7d682f4670835300970ab1a18b7f9a_690x546.png) 已上的方法,如不能解决您的问题请参考:[https://blog.csdn.net/joyhen/article/details/21631833](https://blog.csdn.net/joyhen/article/details/21631833)