Using the Nginx reverse proxy to avoid AJAX Cross-domain requests _ajax related

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.