The same-origin policy means that the protocol, host, and Port are the same. We can deploy 2 different ports of Tomcat on the local machine so that one Tomcat app accesses the rest service under another tomcat through Ajax, so there is a cross-domain access problem. Modify the three port numbers below the Conf/server.xml:
<server port= "* * * *" shutdown= "shutdown" > <connector port= "* * *" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/> <connector port= "* * *" protocol= "ajp/1.3" redirectport= "8443"/>
On the basis of the previous blog, in order to solve the CXF cross-domain problem needs:
1. Modify pom file to increase providers
<dependency><groupid>org.apache.cxf</groupid><artifactid>cxf-rt-rs-extension-providers </artifactId><version>${cxf.version}</version></dependency>
2. Modify the Cxf-spring.xml Configuration Jsonp Interceptor
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs= "Http://cxf.apache.org/jaxrs" xsi:schemalocation= " http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd "><jaxrs:server address= "/rest" ><jaxrs:servicebeans><ref bean= "Nameserviceimpl"/></jaxrs:servicebeans>< !--Jsonp--><jaxrs:providers><bean class= " Org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor "/></JAXRS:PROVIDERS><JAXRS: Ininterceptors><bean class= "Org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/></JAXRS: Ininterceptors><jaxrs:outinterceptors><bean class= " Org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor "/></JAXRS:OUTINTERCEPTORS></JAXRS: Server></beans>
3. Access via Ajax under another tomcat
$.ajax ({ type: ' Get ', URL: ' http://127.0.0.1:8080/aty-rest/rest/rest/welcome ', dataType: ' Jsonp ', jsonp: ' _jsonp ', jsonpcallback: ' Callback ', success:function (data) {alert (json.stringify (data); }});
This article refers to the Web service Thing (4)-a good introductory article using CXF to develop rest services.
Jax-rs Development (III): cross-domain issues when Ajax accesses rest services