1. How is cross-domain formed?When we request a URL whose protocol, domain name, or port is different from the protocol, domain name, or port of the current page URL, we call it cross-domain. Cross-domain will lead to: 2. The root cause of cross-domainThe root cause of cross-domain requests is the same-origin policy of the requesting browser, and the reasons for the cross-domain request error are: browser same-origin policy && request is of ajax type && request is indeed cross-domain 3. SolutionIntroduce three methods jsonp, cors, proxy forwarding 1. JSONP JSONP is a common method for cross-origin communication between servers and clients. The biggest feature is its simplicity, applicability and good compatibility (compatible with lower versions of IE). The disadvantage is that it only supports get requests but not post requests. Let's take a very simple example: we request a picture from a network address through the src attribute of the img tag. This is a non-same-origin request, but because the browser's same-origin policy is only valid for ajax requests, our request will not be affected. In other words, only ajax requests will cause cross-domain problems. 2. CORSCORS is the abbreviation of Cross-Origin Resource Sharing. It is a W3C standard and a fundamental solution for cross-origin AJAX requests. CORS allows any type of request. When using CORS to access data, the client does not need to change any data access logic. All the work is done automatically between the server and the browser. The front-end code is no different from sending a normal Ajax request. We only need to set it up on the server side (the back-end is active). 3. Proxy forwarding Set up an intermediate proxy service between the front-end service and the back-end interface service. Its address remains the same as that of the front-end server. Then: Configure the proxy server in the devServer (development environment) of vue.config.js to send requests through this proxy server. This is not a pure cross-domain problem. The code is as follows: module.exports = { devServer: { // ... omitted // Proxy configuration proxy: { // If the request address starts with /api, the proxy mechanism will be triggered // http://localhost:9588/api/login -> http://localhost:3000/api/login '/api': { target: 'http://localhost:3000' // The real interface address we want to proxy} } } } } Remember that the root path in baseURL is a relative address, not an absolute address. The above is the detailed content of the causes and solutions of cross-domain problems in ajax request front-end. For more information about the causes and solutions of ajax cross-domain problems, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Summary of XHTML application in web design study
>>: Docker - Summary of 3 ways to modify container mount directories
01 The concept of parallel replication In the mas...
Table of contents Preface Front-end structure Bac...
This article example shares the specific code of ...
Table of contents Linux MySQL 5.5 upgraded to MyS...
In Linux C/C++, thread-level operations are usual...
Background Threads •Master Thread The core backgr...
<!doctype html> <html xmlns="http:/...
Knowing the IP address of a device is important w...
Being a web designer is not easy. Not only do you...
Preface What is state We all say that React is a ...
The significance of writing order Reduce browser ...
Nginx reverse proxy multiple servers, which means...
1. Installation apt-get install mysql-server requ...
Just pass in custom parameters HTML <div id=&q...
For example: <u> This has no ending characte...