Jetty Cross Origin Filter solutions for jquery ajax access across domains

Source: Internet
Author: User

You might encounter an error like this when using jquery Ajax Post requests

XMLHttpRequest cannot oad http://xxxxxx. Origin http://xxxxxx is isn't allowed by Access-control-allow-origin.

This is an issue with Ajax cross-domain access permissions, and the server side does not accept HTTP requests from a different IP address from a script file. Solving this problem requires a server-side configuration that allows the server side to accept HTTP requests from script files from different domains. A simple workaround is to configure the jetty cross Origin Filter on the server side.

First you need to download Jetty-servlets.jar. http://central.maven.org/maven2/org/eclipse/jetty/jetty-servlets/. Then import the Jetty-servlets.jar into the Web-inf/lib folder. Finally, configuring some simple parameters on the Web. xml file allows the server side to allow cross-domain access. A typical configuration is this:

1 <Web-app>2 3     <Filter>4 5         <Filter-name>Cross-origin</Filter-name>6 7         <Filter-class>Org.eclipse.jetty.servlets.CrossOriginFilter</Filter-class>8 9         <Init-param>Ten  One             <Param-name>Allowedorigins</Param-name> A  -             <Param-value>*</Param-value> -  the         </Init-param> -  -         <Init-param> -  +             <Param-name>Allowedmethods</Param-name> -  +             <Param-value>Get,post,options,delete,put,head</Param-value> A  at         </Init-param> -  -         <Init-param> -  -             <Param-name>Allowcredentials</Param-name> -  in             <Param-value>True</Param-value> -  to         </Init-param> +  -         <Init-param> the  *             <Param-name>Allowcredentials</Param-name> $ Panax Notoginseng             <Param-value>True</Param-value> -  the         </Init-param> +  A     </Filter> the  +     <filter-mapping> -  $         <Filter-name>Cross-origin</Filter-name> $  -         <Url-pattern>/rest/*</Url-pattern> -  the     </filter-mapping> - Wuyi </Web-app>
Jetty Cross Origin configuration method

Next, explain the specific meaning of the parameter:

Allowedorigins: A domain or link address that allows cross-domain access, multiple addresses are separated by commas, and the default value is "*", which means that access requests from all domains are accepted.

Allowedmethods: Acceptable HTTP request method, multiple methods separated by commas, default to get, Psot, HEAD.

Allowcredentials: The default is true if trusted requests are allowed to access resources.

For additional detailed configuration, refer to the official documentation:

Http://www.eclipse.org/jetty/documentation/current/cross-origin-filter.html

Jetty Cross Origin Filter solutions for jquery ajax access across domains

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.