0

So I'm new in angular and I'm new with API's I tried to create thingy, where I can get video information by id, using several tutorials and browsing stackoverflow I managed to find 2 solutions to the problem, but neither of them work for me. First I tried

mvcApp.service('ApiCall', ['$http', function($http) {
var result;
this.GetApiCall = function() {
    result = $http.get('https://hosting.com/webmasters/video_by_id?id=123456789')
        .success(function(data) {
            result = (data);
        })
        .error(function() {
            alert('Something wrong');
        });
    return result;
}}]);

It uses good API link, but return No 'Access-Control-Allow-Origin' header is present on the requested resource error.

Next solution I found was:

mvcApp.factory('json', function($http) {
return {
    getInformation: function (name) {
        var url = 'https://hosting.com/webmasters/video_by_id';
        return $http.jsonp(url, {
            params: {
                id: name
            }
        });
    }
}});

This one returns errors, as it does not recognises var as a link, and return video_by_id?id=123456789:1 Uncaught SyntaxError: Unexpected token : error. But as I tinkered with it and looked at some other examples, found out that adding extension to links, fixes this, but I do not have or know an extension. So any help would be valuable

Dona Tas
  • 3
  • 1
  • 3
  • I dont see _thumbsize_ in your code – roberto tomás Apr 05 '17 at 20:38
  • Edited. I removed it, as it was not essential for the problem – Dona Tas Apr 05 '17 at 20:39
  • hey, that `:1` is still there on the end for some reason. Where is that coming from, it doesn't look like your hard-coded query? in the sample code you provide... And this time it is on a different parameter. – roberto tomás Apr 05 '17 at 21:10
  • Well I dont know it either, it might be app doing it, because it does not recognize it as a link. But as soon as I try to use other API's where link have extensions, for example, if I did video_by_id.php?..... that :1 would dissappear – Dona Tas Apr 05 '17 at 21:20
  • And it does not depend on parameter, it ats it to the end – Dona Tas Apr 05 '17 at 22:10
  • Check to see if elsewhere in the code there is a call adding an interceptor like $httpProvider.interceptors – roberto tomás Apr 06 '17 at 11:56

1 Answers1

0

If your API is not in the same server as your Angular code, you could handle CORS on the requested resource, to avoid the error.

Community
  • 1
  • 1