Access-Control-Allow-Origin in ajax call to jersey rest web services(ajax 调用 jersey rest web 服务中的 Access-Control-Allow-Origin)
问题描述
我使用 jersey JAX-Rs 作为 Web 服务来查询 mysql.我尝试通过混合移动应用程序使用 Web 服务.
I used jersey JAX-Rs as web service to query mysql.I try to consume the web-service via hybrid mobile application .
我参考这个http://coenraets.org/blog/2011/11/building-restful-services-with-java-using-jax-rs-and-jersey-sample-application/#comment-440541
在服务器端运行以下代码在tomcat server7中查询sql
In server side the following code running in tomcat server7 to query sql
@Path("/employees")
public class EmployeeResource {
EmployeeDAO dao = new EmployeeDAO();
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<Employee> findAll() {
return dao.findAll();
}
}
公共类 EmployeeDAO {
public class EmployeeDAO {
public List<Employee> findAll() {
List<Employee> list = new ArrayList<Employee>();
Connection c = null;
String sql = "SELECT e.id, e.firstName, e.lastName, e.title FROM employee as e";
try {
c = ConnectionHelper.getConnection();
Statement s = c.createStatement();
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
list.add(processSummaryRow(rs));
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
ConnectionHelper.close(c);
}
return list;
}
protected Employee processSummaryRow(ResultSet rs) throws SQLException {
Employee employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setFirstName(rs.getString("firstName"));
employee.setLastName(rs.getString("lastName"));
employee.setTitle(rs.getString("title"));
/*employee.setPicture(rs.getString("picture"));
employee.setReportCount(rs.getInt("reportCount"));*/
return employee;
}
}
我已经创建了数据库目录,表名作为员工,字段 id,firstName,lastName,title.
I have been created the database directory with table name as employee with fields id,firstName,lastName,title.
现在我在 same project 的 web-content 文件夹中有 html 文件.
Now i have the html file in the web-content folder of the same project .
<!DOCTYPE HTML>
<html>
<head>
<title>Employee Directory</title>
</head>
<body>
<h1>Employee Directory</h1>
<ul id="employeeList"></ul>
<script src="http://code.jquery.com/jquery-1.7.min.js"></script>
<script src="js/employeelist.js"></script>
</body>
</html>
员工名单
getEmployeeList();
function getEmployeeList() {
$.getJSON(serviceURL + 'employees', function(data) {
$('#employeeList li').remove();
var employees = data.employee;
$.each(employees, function(index, employee) {
$('#employeeList').append(
'<li><a href="employeedetails.html#' + employee.id + '">'
+ employee.firstName + ' ' + employee.lastName + ' ('
+ employee.title + ')</a></li>');
});
});
}
这将在索引页面中显示准确的员工详细信息.
This will shows the extact employee details in the Index page.
现在我已经在 another project 中创建了一个 html 页面,我将在其中放置与上面指定的相同的 $.getJSON 调用将在控制台中引发错误
Now i have been create an html page in another project where i will put same $.getJSON call which specified above will throw error in console as
XMLHttpRequest cannot load http://localhost:8181/jQueryJAXRS/rest/employees. Origin null is not allowed by Access-Control-Allow-Origin.
实际上我尝试使用客户端和服务器基础架构开发应用程序.所以我需要在客户端中有 html 文件才能在服务器端使用球衣网络服务.如果我制作 $.getJSON 或 $.ajax 真的很有帮助从另一个项目中的 html 文件调用以使用 Web 服务.
Actually I try to develop an application with client and server infrastructure .So i need to have html files in the client to consume the jersey web-services in serverside.It is really helpful if i make $.getJSON or $.ajax call from the html file in another project inorder to consume the web service.
when i use this url http://localhost:8181/jQueryJAXRS/rest/employees in my browser
显示xml
<employees>
<employee>
<firstName>john</firstName>
<id>1</id>
<lastName>doe</lastName>
<reportCount>0</reportCount>
<title>trainee</title>
</employee>
<employee>
<firstName>james</firstName>
<id>2</id>
<lastName>dane</lastName>
<reportCount>0</reportCount>
<title>developer</title>
</employee>
<employee>
<firstName>ramsy</firstName>
<id>4</id>
<lastName>stuart</lastName>
<reportCount>0</reportCount>
<title>QA</title>
</employee>
</employees>
但是当我尝试通过脚本方式时,它会显示发生 CORS 错误.提出一些想法对我很有帮助.
but when i try through script way it will show CORS error occurs. Suggest some ideas will really help me.
提前致谢.
推荐答案
你必须使用 CORS一个>.
为此:
- 你必须使用 cors-过滤罐
那么你必须在你的 web.xml 中使用一个过滤器:
- You have to use cors-filter jar
Then you have to use a filter in your web.xml :
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowGenericHttpRequests</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowSubdomains</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, HEAD, POST, OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Content-Type, X-Requested-With</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>X-Test-1, X-Test-2</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.maxAge</param-name>
<param-value>-1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/EmployeeResource</url-pattern>
</filter-mapping>
这篇关于ajax 调用 jersey rest web 服务中的 Access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ajax 调用 jersey rest web 服务中的 Access-Control-Allow-Origin
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01