1

私はAngularプロジェクトのテスト実行フレームワークとしてカルマを使用しています。Angularサーバーサービスは、http://localhost:8081/common/countries国に関するすべての情報を取得するなどのデータを取得するために別のWeb URLにアクセスする必要があります。私の問題は私のカルマの始まりであり、ブラウズ同じオリジンポリシーによってこの原因クロスドメインの問題localhost:9876からデータを取得する必要があります。http://localhost:8081/common/countriesそのため、コンソールに以下のエラーが表示されます。

Error: Unexpected request: GET http://localhost:8081/common/countries
No more request expected
    at Error (<anonymous>)
    at $httpBackend (http://localhost:9876/absoluteC:/WebUI/WebUI/test/lib/angular/angular-mocks.js:934:9)
    at sendReq (http://localhost:9876/absoluteC:/WebUI/WebUI/vendor/angular/angular.js:9087:9)
    at $http (http://localhost:9876/absoluteC:/WebUI/WebUI/vendor/angular/angular.js:8878:17)
    at Object.getMock (http://localhost:9876/base/share/services.js:644:17)
    at Object.get (http://localhost:9876/base/share/services.js:347:28)
    at Object.getCountries (http://localhost:9876/base/share/services.js:221:22)
    at Object.clSingleSelectConfig.nationality.getData (http://localhost:9876/base/share/directives.js:146:32)
    at http://localhost:9876/base/share/directives.js:192:44
    at nodeLinkFn (http://localhost:9876/absoluteC:/WebUI/WebUI/vendor/angular/angular.js:4360:13) 

私が試したこと: 1 カルマ プラグイン karma-chrome-launcher をインストール--disable-web-securityし、構成ファイルに追加します。しかし、うまくいきません。2 ヘッダーに「Access-Control-Allow-Origin」「Access-Control-Allow-Headers」「Access-Control-Allow-Methods」を設定して、サーバー応答でオリジン アクセスを許可します。

上記のすべてが機能しないので、どうすれば問題を解決できますか?

4

1 に答える 1

0

クロスドメイン リクエストの場合は、 expectJSONPを使用し、必ずコールバック パラメーターを使用してください。

describe('stackoverflow.activity tests', function () {
    var svc, httpBackend;

    beforeEach(function (){  
      module('ngResource');
      module('stackoverflow.activity');
      inject(function($httpBackend, StackoverflowActivityService) {
        svc = StackoverflowActivityService;      
        httpBackend = $httpBackend;
      });
    });

    afterEach(function() {
      httpBackend.verifyNoOutstandingExpectation();
      httpBackend.verifyNoOutstandingRequest();
    });

    it('should send the message and return the response', function (){
        var returnData = { testing: 'anything'};
        httpBackend.expectJSONP('http://api.stackexchange.com/2.1/users/gigablox/timeline?callback=JSON_CALLBACK').respond(returnData);
        svc.events({
            user:'gigablox',
            params:{
                callback:'JSON_CALLBACK'
            }
        }).get(function(user) {
            expect(user.testing).toEqual('anything');
        });
        httpBackend.flush();
    });
});

この例のソース

于 2013-10-21T02:57:47.747 に答える