Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

One-stop solution to the Cross-domain problem of Java

Shulou Source: shulou.com Published: 2022-06-02 07:50:21 09月10日 Update

This article mainly explains "one-stop solution to cross-domain problems in Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "one-stop solution to cross-domain problems in Java".

(1) js side processing: usually in the way of ajax, the following is the processing code

$.ajax ({type: "GET", url: 'common/goBD.do', xhrFields: {withCredentials: true}, crossDomain: true, success: function (data) {window.open ("http://www.baidu.com");}}))

Here we mainly pay attention to the code: xhrFields: {withCredentials: true}, indispensable; url is the address of the access server. After a successful return, you can jump to the Baidu page (open the tab page newly, of course, you can also open it on the original page).

(2) Server processing: by setting the response header information, the following is the processing code

@ RequestMapping (value= "/ goBD") public void goBD (HttpServletResponse response,HttpServletRequest request) throws IOException {String msg = "; String result =" {\ "msg\": "+ msg+"} "; PrintWriter out = response.getWriter (); response.setHeader (" Access-Control-Allow-Credentials "," true "); response.setHeader (" Access-Control-Allow-Origin ", request.getHeader (" Origin ")) Response.setHeader ("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader ("Access-Control-Max-Age", "0"); response.setCharacterEncoding ("utf-8"); out = response.getWriter (); out.write (result); out.flush (); out.close ();}

Here, we simply deal with the response header information, and do not add anything else. If there are other judgments, such as login, you can enter the user name and password on the js side for judgment processing, and make different processing in the front end by returning the value.

Thank you for your reading. the above is the content of "one-stop solution to the cross-domain problem of Java". After the study of this article, I believe you have a deeper understanding of the one-stop solution to the cross-domain problem of Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Processing problems code learning information content headers methods pages services different successful front-end just available address password that is ideas situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple OPPO Reno Redmi Shulou Tech Info