3

I have defined the following 2 services in AngularJS. Both should return JSONP since I'm doing a cross domain request.

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

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

In my Controller, I'm binding the result to my scope:

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

According to my console.log() output, B returns the expected result in JSON format, while A returns something like:

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

Am I missing something? What do I have to do, in order to receive proper JSON from A?

2
  • Service B returns the expected result in JSON format, while Service B returns something like: --> do you mean server A ? Commented Nov 5, 2012 at 15:22
  • sry, I had a typo here. Service A returns the error. Service B works fine Commented Nov 5, 2012 at 15:29

1 Answer 1

12

Your code looks confusing. Both services were called A but you use different module names. Apart from that, does it matter that your second service calls a JSON file whereas the first one doesn't?

I would try the following:

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'} }
       }
    );
  });
Sign up to request clarification or add additional context in comments.

4 Comments

@mawo So the problem is solved or does it still exist? I would then check if the error depends on the JSON resource you are querying or if its AngularJS.
problem solved. it turned out that the server did not return valid jsonp. thanks anyway!
how does one inject it when its coded this way, i.e. im trying to call it in my controller, but i cant just controller(abc', ['$scope','app.services', funcion
ok, i thnjk i know where im going wrong there, i was trying to put servicesin the list in controller() instead of the list in module(), however im cointinually getting errors on the file app.js which contains my controller, where i try to use this module. can you give an example of how you call and use it in the controlle rplease, for your module suggestion? @F Lekschas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.