3

AngularJS で次の 2 つのサービスを定義しました。クロスドメイン リクエストを行っているため、どちらも JSONP を返す必要があります。

サービス A:

angular.module('ServiceA', ['ngResource']).
  factory('A', function ($resource) {
     return $resource('url/offers', {},
       {
         get: { method: 'JSONP', params: {property_code: 'DEMO_ERFOLGX', adults: '2',
                callback: 'JSON_CALLBACK'} }
       }
    );
  });

サービス B:

angular.module('ServiceB', ['ngResource']).
  factory('B', function ($resource) {
     return $resource('url/search.json', {},
       {
         get: { method: 'JSONP', params: {property_code: 'DEMO_ERFOLGX', adults: '2',
                callback: 'JSON_CALLBACK'} }
       }
    );
  });

私のコントローラーでは、結果をスコープにバインドしています。

$scope.foo = A.get();  
$scope.bar = B.get();

私の console.log() の出力によると、B は予想される結果を JSON 形式で返しますが、A は次のようなものを返します。

SyntaxError: invalid label
{"DEMO_ERFOLGX":{"offers":[{"checkin":"2012-12-01","checkout"

何か不足していますか?A から適切な JSON を受け取るにはどうすればよいですか?

4

1 に答える 1

12

あなたのコードは紛らわしいようです。両方のサービスはAと呼ばれていましたが、異なるモジュール名を使用しています。それとは別に、2番目のサービスがJSONファイルを呼び出すのに対し、最初のサービスは呼び出さないことは重要ですか?

私は次のことを試みます:

angular.module('app.services', ['ngResource'])
  .factory('ServiceA', function ($resource) {
     return $resource('url/offers', {},
       {
         get: { method: 'JSONP', params: {property_code: 'DEMO_ERFOLGX', adults: '2',
                callback: 'JSON_CALLBACK'} }
       }
    );
  });
  .factory('ServiceB', function ($resource) {
     return $resource('url/search.json', {},
       {
         get: { method: 'JSONP', params: {property_code: 'DEMO_ERFOLGX', adults: '2',
                callback: 'JSON_CALLBACK'} }
       }
    );
  });
于 2012-11-05T16:23:04.303 に答える