When we are developing a project, we generally use ajax to request data, but when using ajax, there will be cross-domain problems.
Why do cross-domain problems occur?
The cross-domain problem arises from the same-origin policy of JavaScript, which means that access is allowed only when the domain name, protocol name, and port number are the same, that is, JavaScript only allows access to resource files in its own domain. As shown
How to solve cross domain
There are three ways to solve cross-domain:
- jsonp
When writing ajax, set dataType: jsonp, which means that it allows integrating script tags on the server side and returning them to the client side, which can be accessed across domains through JavaScript callback.
- Just fill in the response headers on the server side
header("Access-Control-Allow-Origin:*");/*The asterisk indicates that all domains are acceptable, */
header("Access-Control-Allow-Methods:GET,POST");
- Proxy (this method is seen on the Internet, please refer to other blogs for details)