Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

feat(loaders): add jsonp support #547

Closed
wants to merge 1 commit into from
Closed

feat(loaders): add jsonp support #547

wants to merge 1 commit into from

Conversation

juanpujol
Copy link

I needed jsonp support so I did this small patch. Very easy to use. Just pass method: 'JSONP' as an option on $translateUrlLoader.

$translateUrlLoader({
  key: 'de_DE',
  url: 'foo/bar.json?callback=JSON_CALLBACK',
  method: 'JSONP'
 });
$translateProvider.useLoader('$translatePartialLoader', {
  urlTemplate: 'https://your-site/translation/{part}?locale={lang}&callback=JSON_CALLBACK',
  method: 'JSONP'
});

Works on loader-partial and loader-url.

Cheers.

Adds support to make requests using jsonp method.
Works on loader-url and loader-partial.

```javascript
$translatePartialLoader({
    key: 'en',
	urlTemplate: '/locales/{part}-{lang}.json?callback=JSON_CALLBACK',
	method: 'JSONP'
});
```
@knalli
Copy link
Member

knalli commented May 8, 2014

@juanpujol Yes, that's looking great!

@PascalPrecht Should we not change the api to something like this?

   return function (options) {

      if (!options || !options.url) {
        throw new Error('Couldn\'t use urlLoader since no url is given!');
      }

      var deferred = $q.defer();

      $http(angular.extend({
        url: options.url,
        params: { lang: options.key },
        method: 'GET'
      }, options.options || {})).success(function (data) {
        deferred.resolve(data);
      }).error(function (data) {

ephigabay pushed a commit to ephigabay/angular-translate that referenced this pull request Jun 2, 2014
@ephigabay ephigabay mentioned this pull request Jun 2, 2014
@0x-r4bbit
Copy link
Member

@knalli yes! Do you wanna do it?

@mateusleon
Copy link

Hello there! This pull request will be made? I was searching for a solution and this pull request will made it! Can you please merge this patch? Thanks a lot.

@knalli
Copy link
Member

knalli commented Sep 18, 2014

As of b685601 (and a small fixup 8613bef), you should be able to do this with any loader you want. Perhaps do you want to check out?

@juanpujol
Copy link
Author

Sorry, is not clear how to use that $http on the loader. I want to load my parts via jsonp. I tried this:

$translatePartialLoaderProvider.addPart('3');
$translateProvider.useLoader('$translatePartialLoader', {
  urlTemplate: translateUrl + '/api/translation/{part}?locale{lang}&callback=JSON_CALLBACK',
  $http: {    
    method: 'JSONP'
  }
});

But I still get the No 'Access-Control-Allow-Origin', so is not working.

Can somebody help me please?

Thank you.

@knalli
Copy link
Member

knalli commented Sep 30, 2014

Well, that's actually all you need. Do you use the latest 2.4? 'Access-Control-Allow-Origin' does mean nothing*, the question is only if the requested was made via JSONP.

  • Appendix: I mean, it is irrelevant for this issue. If the request is made as JSONP, the options were put to AJS correctly. Then it is up to the server. Generally, the requests look like url&jsonp=callback

@juanpujol
Copy link
Author

Is not working. Before I had a forked version of the partial, forcing the method attribute. (https://github.com/juanpujol/bower-angular-translate-loader-partial/blob/master/angular-translate-loader-partial.js#L20) This worked fine, but I want to use the new v2.4 solution now.

@knalli
Copy link
Member

knalli commented Sep 30, 2014

Can you track this down on your own? It looks like I have accidentally swapped argument 2 and 3 here

$http(angular.extend({}, $httpOptions, {
method : 'GET',
url: this.parseUrl(urlTemplate, lang)
})).success(function(data){

@juanpujol
Copy link
Author

Yes, just swapped back the arguments and it worked like a charm =)

$http(angular.extend({}, {
  method: 'GET',
  url: this.parseUrl(urlTemplate, lang)
}, $httpOptions))

Thank You.

knalli added a commit that referenced this pull request Sep 30, 2014
This adds also some tests for the loader options (here: $http.method)

Fixes #754
Fixes #547
@knalli knalli closed this in 0c35a95 Oct 3, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants