Access-Control-Allow-Origin在ajax调用泽西restWeb服务中

我使用泽西JAX-Rs作为Web服务来查询mysql.I尝试通过混合移动应用程序使用Web服务。

我推荐这个http://coenraets.org/blog/2011/11/building-restful-services-with-java-using-jax-rs-and-jersey-sample-application/#comment-440541

在服务器端,以下代码在tomcat server7中运行,以查询sql

@Path("/employees") public class EmployeeResource { EmployeeDAO dao = new EmployeeDAO(); @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public List findAll() { return dao.findAll(); } } 

公共类EmployeeDAO {

 public List findAll() { List list = new ArrayList(); 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; } 

}

我已经创建了数据库目录,其表名为employee,其中包含字段id,firstName,lastName,title。

现在我在同一个项目的web-content文件夹中有html文件。

    Employee Directory   

Employee Directory

    员工主义剧本

     getEmployeeList(); function getEmployeeList() { $.getJSON(serviceURL + 'employees', function(data) { $('#employeeList li').remove(); var employees = data.employee; $.each(employees, function(index, employee) { $('#employeeList').append( '
  • ' + employee.firstName + ' ' + employee.lastName + ' (' + employee.title + ')
  • '); }); }); }

    这将在索引页面中显示extact员工详细信息。

    现在我已经在另一个项目中创建了一个html页面,我将在上面指定的相同$ .getJSON调用将在控制台中抛出错误

      XMLHttpRequest cannot load http://localhost:8181/jQueryJAXRS/rest/employees. Origin null is not allowed by Access-Control-Allow-Origin. 

    实际上我尝试使用客户端和服务器基础结构开发应用程序。因此我需要在客户端中使用html文件来使用服务器端的jersey web服务。如果我从$ .getJSON或$ .ajax调用,那将非常有用。另一个项目中的html文件,以便使用Web服务。

     when i use this url http://localhost:8181/jQueryJAXRS/rest/employees in my browser 

    它显示了xml

       john 1 doe 0 trainee   james 2 dane 0 developer   ramsy 4 stuart 0 QA   

    但是当我尝试通过脚本方式时,它将显示CORS错误发生。 建议一些想法真的会对我有所帮助。

    提前致谢 。

    你必须使用CORS 。

    为了那个原因:

    1. 你必须使用cors-filter jar
    2. 然后,您必须在web.xml中使用filter:

        CORS com.thetransactioncompany.cors.CORSFilter  cors.allowGenericHttpRequests true   cors.allowOrigin *   cors.allowSubdomains true   cors.supportedMethods GET, HEAD, POST, OPTIONS   cors.supportedHeaders Content-Type, X-Requested-With   cors.exposedHeaders X-Test-1, X-Test-2   cors.supportsCredentials true   cors.maxAge -1    CORS /EmployeeResource  

    感谢Jhanvi建议这个CORS的想法。 在这里,我解释更多,以获得更好的清晰度,并为此问题提供完整的解决方案

    我引用此链接来解决此问题

    CORS JAR

    下载CORS jar和java-property jar。将这些jar放在tomcat server的lib文件夹中。然后在web.xml中添加以下filter作为web-app的chlidnode。

      CORS com.thetransactioncompany.cors.CORSFilter   CORS /*  

    该解决方案将解决CORS问题。

    谢谢你的回答! 它引导我使用maven导入jar的简短方法,并将依赖项添加到pom.xml

      com.thetransactioncompany java-property-utils 1.9.1   com.thetransactioncompany cors-filter 1.9.1  

    检查当前版本:
    http://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter/1.9 http://mvnrepository.com/artifact/com.thetransactioncompany/java-property-utils/1.9