小编Pau*_*hon的帖子

启用AngularJS到Jersey的CORS发布请求

我正在尝试将一个JSON文档从AngularJS应用程序发布到Jersey REST服务.请求失败,通知我:

XMLHttpRequest cannot load http://localhost:8080/my.rest.service/api/order/addOrder. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

Jersey REST Post功能

我启用了(我认为相应的)适当的标头:Access-Control-Allow-OriginAccess-Control-Allow-Methods响应,如下面的方法所示:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/addOrder")
public Response addOrder(DBObject dbobject) {
    DB db = mongo.getDB("staffing");
    DBCollection col = db.getCollection("orders");
    col.insert(dbobject);
    ObjectId id = (ObjectId)dbobject.get("_id");
    return Response.ok()
            .entity(id)
            .header("Access-Control-Allow-Origin","*")
            .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
            .allow("OPTIONS")
            .build();
}
Run Code Online (Sandbox Code Playgroud)

Angular JS控制器

我已经声明了应用程序,并使用类似Stack Overflow问题中建议的所有设置配置了$ httpProvider:

var staffingApp = angular.module('myApp', ['ngRoute', 'ui.bootstrap']);
myApp.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.useXDomain …
Run Code Online (Sandbox Code Playgroud)

java rest jersey cors angularjs

11
推荐指数
1
解决办法
2万
查看次数

标签 统计

angularjs ×1

cors ×1

java ×1

jersey ×1

rest ×1