Nginx reverse proxy is used to avoid cross-origin ajax requests. nginxajax
Nginx + tomcat on the server, where nginx listens to port 80 and tomcat listens to port 8080.
Because I am not familiar with the front-end, I think that callback is not required when ajax is used. However, the front-end students say that callback is not required when cross-origin is not used, so that I can add it to the returned json. However, I have just learned the most basic spring-mvc usage and do not know how to add callback.
Some feasible code will be found on the Internet, like this:
@ RequestMapping (method = RequestMethod. GET, value = "getProjectStatusList", produces = "text/html; charset = UTF-8") @ ResponseBodypublic String getProjectStatusList (HttpServletRequest request, HttpServletResponse response) {Map <String, object> map = new HashMap <String, Object> (); try {String callback = request. getParameter ("callback"); // System. out. println ("token:" + request. getHeader ("token"); List <String> list = ss. GetProjectStatusList (); map. put ("status", "success"); map. put ("data", list); ObjectMapper mapper = new ObjectMapper (); // This stitching is important... String result = callback + "(" + mapper. writeValueAsString (map) + ")"; // String result = mapper. writeValueAsString (map); return result;} catch (Exception e) {JSONObject jo = new JSONObject (); jo. put ("status", "fail"); jo. put ("data", e. getMessage (); return jo. toString ();}}
However, this change is really frustrating for me, because I have too many URL ing and the modification cost is too high.
So I think of nginx. Isn't this guy involved in reverse proxy? Really wit me
With this idea, it is easy to do. Add a location directly to the server listening to port 80:
location /myApp {proxy_pass http://localhost:8080/myApp;}
Reload nginx:
{NGINX_HOME}/sbin/nginx -s reload
Then, the previous cross-origin request of http: // site: 8080/myApp is changed to a non-Cross-origin request of http: // site/myApp.
The above section describes how to use Nginx reverse proxy to avoid ajax cross-origin requests. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!