Jax-rs Development (III): cross-domain issues when Ajax accesses rest services

Source: Internet
Author: User
Tags server port

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 "/&GT;&LT;/JAXRS:PROVIDERS&GT;&LT;JAXRS: Ininterceptors><bean class= "Org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor"/&GT;&LT;/JAXRS: Ininterceptors><jaxrs:outinterceptors><bean class= " Org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor "/&GT;&LT;/JAXRS:OUTINTERCEPTORS&GT;&LT;/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

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.