首页 \ 问答 \ 使用CORS跨域REST / Jersey Web服务(Cross domain REST/Jersey web services with CORS)

使用CORS跨域REST / Jersey Web服务(Cross domain REST/Jersey web services with CORS)

我想用CORS(跨源资源共享)制作跨域REST Web服务。 我正在使用泽西图书馆提供服务。

我需要知道

  1. 从服务器端的角度来看,我需要做哪些代码/配置更改?

  2. 如何从HTML5 / js调用这些服务。

谢谢


I want to make Cross domain REST web services with CORS(Cross-Origin Resource Sharing). I am using Jersey Libraries for making services.

I need to know

  1. What code/configuration changes i need to do from server side perspective?

  2. How to invoke this services from HTML5/js.

Thanks


原文:https://stackoverflow.com/questions/9928266
更新时间:2024-01-15 08:01

最满意答案

您可以使用一个ng-repeat并使用customers[$index]访问客户对象数组。

请看下面的演示或这个jsfiddle

angular.module('App', [])
.controller("myCtrl", function($scope){ 
  $scope.tickets = [ {code:'1', city:'Istanbul', value:10.0},
                     {code:'2', city:'London', value:50.0},
                     {code:'3', city:'Paris', value:30.0}
                    ]; 
  $scope.customers = [ {name:'Customer1', age:36},
                       {name:'Customer2', age:42},
                       {name:'Customer3', age:28}
                     ];

  $scope.array = [];

  $scope.init = function(){
    var k = 0;
    for(; k < $scope.tickets.length && k < $scope.customers.length; k++)
      $scope.array.push({ticket:$scope.tickets[k], customer:$scope.customers[k]});

    for(; k < $scope.tickets.length; k++)
      $scope.array.push({ticket:$scope.tickets[k], customer:{name:'',age:''}});
    for(; k < $scope.customers.length; k++)
      $scope.array.push({ticket:{code:'',city:'',name:''}, customer:$scope.customers[k]});
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="App" ng-controller="myCtrl" ng-init="init()">
    <h2>table with js mapping</h2>
<table border=1>
  <thead>
    <tr>
      <th>Position</th>
      <th>Ticket</th>
      <th>Customer</th>
    </tr>
  </thead>
  <tbody ng:repeat="element in array">
    <tr><td align=center>{{$index}}</td>
        <td align=center>
            {{element.ticket.code}}<br/>
            <strong>{{element.ticket.city}}</strong><br/>
          <em>{{element.ticket.value | currency}}</em>
        </td>
        <td  align=center>
            {{element.customer.name}}<br/>
          {{element.customer.age}}
        </td>
    </tr>
  </tbody>
</table>
       
    <h2>table with ng-repeat</h2>
    <table border=1>
  <thead>
    <tr>
      <th>Position</th>
      <th>Ticket</th>
      <th>Customer</th>
    </tr>
  </thead>
  <tbody ng:repeat="ticket in tickets">
    <tr><td align=center>{{$index}}</td>
        <td align=center>
            {{ticket.code}}<br/>
            <strong>{{ticket.city}}</strong><br/>
          <em>{{ticket.value | currency}}</em>
        </td>
        <td  align=center>
            {{customers[$index].name}}<br/>
          {{customers[$index].age}}
        </td>
    </tr>
  </tbody>
</table>
      </div>


You could use one ng-repeat and access your customers object array with customers[$index].

Please have a look at the demo below or in this jsfiddle.

angular.module('App', [])
.controller("myCtrl", function($scope){ 
  $scope.tickets = [ {code:'1', city:'Istanbul', value:10.0},
                     {code:'2', city:'London', value:50.0},
                     {code:'3', city:'Paris', value:30.0}
                    ]; 
  $scope.customers = [ {name:'Customer1', age:36},
                       {name:'Customer2', age:42},
                       {name:'Customer3', age:28}
                     ];

  $scope.array = [];

  $scope.init = function(){
    var k = 0;
    for(; k < $scope.tickets.length && k < $scope.customers.length; k++)
      $scope.array.push({ticket:$scope.tickets[k], customer:$scope.customers[k]});

    for(; k < $scope.tickets.length; k++)
      $scope.array.push({ticket:$scope.tickets[k], customer:{name:'',age:''}});
    for(; k < $scope.customers.length; k++)
      $scope.array.push({ticket:{code:'',city:'',name:''}, customer:$scope.customers[k]});
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="App" ng-controller="myCtrl" ng-init="init()">
    <h2>table with js mapping</h2>
<table border=1>
  <thead>
    <tr>
      <th>Position</th>
      <th>Ticket</th>
      <th>Customer</th>
    </tr>
  </thead>
  <tbody ng:repeat="element in array">
    <tr><td align=center>{{$index}}</td>
        <td align=center>
            {{element.ticket.code}}<br/>
            <strong>{{element.ticket.city}}</strong><br/>
          <em>{{element.ticket.value | currency}}</em>
        </td>
        <td  align=center>
            {{element.customer.name}}<br/>
          {{element.customer.age}}
        </td>
    </tr>
  </tbody>
</table>
       
    <h2>table with ng-repeat</h2>
    <table border=1>
  <thead>
    <tr>
      <th>Position</th>
      <th>Ticket</th>
      <th>Customer</th>
    </tr>
  </thead>
  <tbody ng:repeat="ticket in tickets">
    <tr><td align=center>{{$index}}</td>
        <td align=center>
            {{ticket.code}}<br/>
            <strong>{{ticket.city}}</strong><br/>
          <em>{{ticket.value | currency}}</em>
        </td>
        <td  align=center>
            {{customers[$index].name}}<br/>
          {{customers[$index].age}}
        </td>
    </tr>
  </tbody>
</table>
      </div>

相关问答

更多