Nginx + tomcat on the server, where Nginx listens on port 80 and Tomcat listens on 8080 ports.
Because the front end is unfamiliar, thought that uses the Ajax to be possible does not need callback, however the front end schoolmate said does not need to cross the domain the case to be callback, let me add in the return JSON. But I have just learned the most basic SPRING-MVC usage, do not know how to add callback
Find some workable code on the web, almost like this:
@RequestMapping (method=requestmethod.get,value= "Getprojectstatuslist", produces= "Text/html;charset=utf-8")
@ResponseBody public
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 the focus ...
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 to me is simply bones, because I have too many URL mapping, the cost of modification is too great.
So witty I thought of the nginx, this guy is not to take to do the reverse agent? It's like my wit.
With this idea, it's easy to do. Add a location directly to the server listening on port 80:
Location/myapp {
proxy_pass http://localhost:8080/myApp;
}
Reload Nginx:
{Nginx_home}/sbin/nginx-s Reload
The Cross-domain request that was previously http://site:8080/myApp was then transformed into a http://site/myApp non cross-domain request.
The above is a small set to introduce the use of Nginx reverse proxy to avoid Ajax Cross-domain request method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!