Jax-rs Development (III): cross-domain issues with AJAX access to rest services and JSONP solutions __js

Source: Internet
Author: User
Tags server port

Homology 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 application accesses the rest service under another tomcat through Ajax, and there is a cross-domain access problem. Modify the three port numbers below 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 a previous blog, in order to solve CXF cross-domain problems, you need to:

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 Cxf-spring.xml Configuration Jsonp Interceptor

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs= "Http://cxf.apache.org/jaxrs" xsi:schemalocation= "
	Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://cxf.apache.org/jaxrs http://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:o utinterceptors> <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 is a reference to the Web Service (4)--A good primer for using CXF to develop rest services.



Related Article

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.