Detailed explanation of cross domain problems and precautions in handling Ajax requests in spring MVC
preface
Sometimes when the front and back ends do data interaction, they will encounter annoying cross domain request problems. If you are still a programming white, it is undoubtedly very painful.
Of course, there must be some solutions online. But their own strength is limited. They may not understand it and can solve the problem. Therefore, the following article will summarize and introduce the cross domain issues and some precautions for handling Ajax requests in spring MVC. I won't say much. Let's take a look at the detailed introduction together.
Why cross domain
Simply put, the browser restricts access to the JS code under site a and makes Ajax requests for the URL under site B. If the current domain name is www.abc COM, the JS code running in the current environment cannot access www.zzz.com under normal circumstances for security reasons Resources under the COM domain name.
resolvent
. First of all, you should know that the core of Ajax request is javascrip object and XMLHttpRequest, while the core of browser request is browser
Ajax request
Browser request
Scenario 1: use ajax to get the user in the session (cross domain)
As can be seen from the above figure, AJAX does not carry cookies every time it accesses the server, so the server will allocate a new session object every time. Therefore, AJAX default requests cannot directly obtain the data in the session. Therefore, if we want to obtain the session of the current browser through Ajax, we must ensure that we carry a cookie every time we send a request to ensure that the obtained events are the same session.
Solution: step1 Server settings receive cookies sent by Ajax
step2 。 Set the Ajax request to carry cookies
Note: xhrfields: {withcredentials: true}, be sure to add!!!!
So you can play happily!
Scenario 2: when the user needs to verify whether the session is logged in (if there is no login, jump to the login page)
Based on scenario 1, the callback problem has been solved, but in using spring MVC
Will report an error. Because when the server forwards or redirects, it returns a page, because only the browser can parse the display page, but the Ajax object cannot, so an error will be reported.
Solution: because the Ajax request object cannot parse the display page, it cannot be forwarded or redirected directly. At this time, we need to bypass the server and directly use the browser to jump to the page. However, for the session expired (the user did not log in), we need to give the browser a feedback. We can give the browser an error code greater than or equal to 600
At the same time, set global options for Ajax
In this way, you can jump to the page through the browser!
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.