访问控制 - 允许 - 原产地在新泽西州REST Web服务调用Ajax [英] Access-Control-Allow-Origin in ajax call to jersey rest web services

查看:125
本文介绍了访问控制 - 允许 - 原产地在新泽西州REST Web服务调用Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的球衣JAX-RS作为Web服务来查询mysql.I试图通过消耗混合移动应用程序的Web服务。

I used jersey JAX-Rs as web service to query mysql.I try to consume the web-service via hybrid mobile application .

我是指这个 <一href="http://coenraets.org/blog/2011/11/building-restful-services-with-java-using-jax-rs-and-jersey-sample-application/#comment-440541" rel="nofollow">http://coenraets.org/blog/2011/11/building-restful-services-with-java-using-jax-rs-and-jersey-sample-application/#comment-440541

在服务器端以下code在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数据库目录,名字,姓氏,职务。

I have been created the database directory with table name as employee with fields id,firstName,lastName,title.

现在我必须在同一项目的Web内容文件夹中的HTML文件

<!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>

EmployeeList的脚本

employeelist script

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>');
                });
            });
        }

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

This will shows the extact employee details in the Index page.

现在我一直在建立一个HTML页面的另一个项目在这里,我将它放在上面规定将扔在控制台误差

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文件,在客户端使用的球衣网络服务serverside.It是真正有用的,如果我做$ .getJSON或$阿贾克斯从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

it shows 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

有关的:

  1. 您必须使用<一个href="http://grep$c$c.com/snapshot/repo1.maven.org/maven2/com.thetransactioncompany/cors-filter/1.3.2"相对=nofollow> CORS过滤罐
  2. 然后,你必须使用过滤器在你的web.xml:

  1. You have to use cors-filter jar
  2. 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>

这篇关于访问控制 - 允许 - 原产地在新泽西州REST Web服务调用Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆