Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 120 posts 448 karma points
    Jun 30, 2014 @ 22:14
    Tom
    0

    angularjs - get data from webservice on other domain/server

    hi,

    I'm trying to get some data to a custom property editor from a remote server, which runs a webservice. The webservice requires credentials and these should be hidden for the users.

    Where do i start?

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jun 30, 2014 @ 22:49
    Jan Skovgaard
    0

    Hi Tom

    What webservice is it? And what does your current code look like?

    /Jan

  • Tom 120 posts 448 karma points
    Jun 30, 2014 @ 22:55
    Tom
    0

    For starters I'm just trying to reach a public webservice. I've made this resource:

    angular.module('umbraco.resources').factory('randomDataResource',
            function ($q, $http) {
                return {
                    getData: function () {
                        return $http.get("http://geo.oiorest.dk/adresser.xml", {
                            params: {
                                postnr: 2400,
                                husnr:5
                            }
                        });
                    }
                };
            }
        );
    

    And then I'm calling the resource like this:

     randomDataResource.getData().then(function (data) {
            var testData = data;
        });
    

    It seem like this is not the right way to do it - I'm getting this error:

    No 'Access-Control-Allow-Origin' header is present on the requested resource

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Jul 01, 2014 @ 08:29
    Richard Soeteman
    101

    Hi Tom,

    I would not call the webservice directly because your authentication details are in visible in Javascript. What I would do is create a WebAPI Controller. Let that WebAPI Controller communicate with the Webservice that needs the authentication. You can even cache results then or do other modification on the data before sending the result back to Angular.

    If you need a real world example how to use a WEbAPI Controller in combination with Angular in Umbraco check this Blog post from Warren Buckley http://creativewebspecialist.co.uk/2013/12/12/umbraco-7-creating-a-generic-settings-editor-with-webapi-angularjs/.

    Hope this helps,

    Richard

  • Tom 120 posts 448 karma points
    Jul 01, 2014 @ 08:44
    Tom
    0

    Hi Richard,

    Thanks a lot! Though I've already figured out that I needed to create a WebApi controller instead, the blog post is a great resource.

    Now I just have to connect to the webservice...

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Jul 01, 2014 @ 08:51
    Richard Soeteman
    2

    Hi Tom,

    HTTPClient is your friend. You might want to check out this article for a complete example.

    http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

    Cheers,

    Richard

Please Sign in or register to post replies

Write your reply to:

Draft