当使用jQuery Ajax post请求时可能会遇到类似这样的错误提示

XMLHttpRequest cannot oad http://xxxxxx. Origin http://xxxxxx is not allowed by Access-Control-Allow-Origin.

这是Ajax跨域访问权限的问题,服务器端不接受来自另一个不同IP地址的由脚本文件发出的http请求。解决这个问题需要在服务器端进行配置使服务器端可以接受来自不同域的脚本文件的http请求。一个简单的解决方法是在服务器端配置Jetty Cross Origin Filter。

首先你需要下载jetty-servlets.jar。下载地址http://central.maven.org/maven2/org/eclipse/jetty/jetty-servlets/ 。然后将jetty-servlets.jar导入到WEB-INF/lib文件夹。最后在web.xml文件配置一些简单的参数就可以使服务器端允许跨域访问。一个典型的配置是这样的:

技术分享
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>
10
11 <param-name>allowedOrigins</param-name>
12
13 <param-value>*</param-value>
14
15 </init-param>
16
17 <init-param>
18
19 <param-name>allowedMethods</param-name>
20
21 <param-value>GET,POST,OPTIONS,DELETE,PUT,HEAD</param-value>
22
23 </init-param>
24
25 <init-param>
26
27 <param-name>allowCredentials</param-name>
28
29 <param-value>true</param-value>
30
31 </init-param>
32
33 <init-param>
34
35 <param-name>allowCredentials</param-name>
36
37 <param-value>true</param-value>
38
39 </init-param>
40
41 </filter>
42
43 <filter-mapping>
44
45 <filter-name>cross-origin</filter-name>
46
47 <url-pattern>/rest/*</url-pattern>
48
49 </filter-mapping>
50
51 </web-app>
Jetty Cross Origin配置方法
接下来解释参数的具体意义:

allowedOrigins: 允许跨域访问的域名或链接地址,多个地址用逗号分隔,默认值为"*", 表示接受来自所有域的访问请求。

allowedMethods: 可接受的http请求方法,多个方法用逗号分隔,默认为GET, PSOT, HEAD.

allowCredentials: 是否允许受信任的请求访问资源,默认为true。

有关其他详细配置可以参考官方文档:

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

CrossOriginFilter的更多相关文章

  1. jetty 最后版本类库树, 基本上大多数应用都够了

    d:\jetty-distribution-8.1.17.v20150415\lib\annotations\javax.annotation-1.1.0.v201108011116.jarjavax ...

  2. cometd的服务器配置

    CometDServlet必须在web.xml中进行配置,如下: <servlet>        <servlet-name>cometd</servlet-name& ...

  3. 深入了解 Dojo 的服务器推送技术

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...

  4. 基于CORS的geoserver同源访问策略

    这个问题理顺整个2天.终于攻克.记录下来. 1.下载文件 首先下载cors压缩包,解压,得到的是org/mortbay/servlets/CrossOriginFilter.class文件,把此文件拷 ...

  5. Java跨域设置

    Access-Control-Allow-Origin 为允许哪些Origin发起跨域请求. 这里设置为"*"表示允许所有,通常设置为所有并不安全,最好指定一下. Access-C ...

  6. java SSM 解决跨域问题

    什么是跨域 跨域是指从一个域名的网页去请求另一个域名的资源.比如从www.baidu.com 页面去请求 www.google.com 的资源.跨域的严格一点的定义是:只要 协议,域名,端口有任何一个 ...

  7. dropwizard使用cors支持跨域浏览器取不到自定义header问题

    dropwizard支持cors的配置如下: public void run(Configuration conf, Environment environment) { // Enable CORS ...

  8. 【Access-Control-Allow-Origin】跨域问题

    [前言] 在实际项目中,可能是多个项目共同完成某个功能,他们之间需要实现数据的交互.这样就会需要有跨域的问题. 比如,发布在不同电脑上的不同项目之间,用不同语言开发的项目之间…… [JSONP] 当使 ...

  9. 关于后台程序java报错问题解决

    HTTP Status – Internal Server Error Type Exception Report Message Handler processing failed; nested ...

随机推荐

  1. JavaWeb项目用浏览器打开网页出现Session Error提示的解决办法

    找到web.xml配置的原始配置的位置: <servlet> <servlet-name>dwr-invoker</servlet-name> <servle ...

  2. CSS shapes布局教程

    文章参考至 一.前言&索引 CSS Shapes布局可以实现不规则的文字环绕效果,需要和浮动配合使用. 兼容性如下图: 还是很不错的,移动端可用,内部中后台项目可用. CSS shapes布局 ...

  3. tf.app.run()的作用

    tf.app.run() 如果你的代码中的入口函数不叫main(),而是一个其他名字的函数,如test(),则你应该这样写入口tf.app.run(test) 如果你的代码中的入口函数叫main(), ...

  4. 安装pytorch

    安装cpu版本的 conda install pytorch-cpu torchvision-cpu -c pytorch 安装gpu版本的 conda install pytorch torchvi ...

  5. MAVLINK协议

    参考https://cloud.tencent.com/developer/news/48344 https://www.cnblogs.com/lovechen/p/5801679.html htt ...

  6. LVS负载均衡软件使用及(LVS简介、三种工作模式、十种调度算法)

    一.LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,目前LVS已经被集成到Linux内核模块中.该项目在Linux内核中实现了基于IP的数据请求负载均衡调度方案, ...

  7. Flask - 运行APP

    from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return 'Hello, ...

  8. Lesson 15 Secrecy in industry

    Why is secrecy particularly important in the chemical industries? Two factors weigh heavily against ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:缩写

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 缩写</title> <lin ...

  10. 解题报告:luogu P5543 [USACO19FEB]The Great Revegetation S

    题目链接:P5543 [USACO19FEB]The Great Revegetation S 好坑啊,都身败名裂了. 思路一: 考虑染色法,跑一遍搜所就好了,不给代码了. 思路二: 考虑并查集,我想 ...