Creating an App for Sharepoint using Dev, AngularJS and RestAPI

2

I need to create an app for sharepoint that does the simple task of listing fields from a list on the screen where the App is placed inside Sharepoint.

I have the following error: In 'Access-Control-Allow-Origin' header is present on the requested resource.

This code is within the default.aspx created by Napa:

<div ng-app>
    <b>Lista simples AngularJS + Sharepoint!</b>
    <div ng-controller="MyController" class="ng-scope">
        <div ng-repeat="p in Products">
            Product Name: {{p.ProductName}} <br />
            Rate: Rs. {{p.ProductRate}} <br />
            <hr />
        </div>
    </div>
</div>

<script>    
    function MyController($scope) {
        $scope.loadREST = function () {
            jQuery.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ProductList')/items?$select=ProductName,ProductRate",
                type: "GET",
                dataType: "JSONP",
                headers: { "Accept": "application/json;odata=verbose" },
                success: function (data) {
                    var newData = [];
                    jQuery.each(data.d.results, function (index, value) {
                        newData.push({ ProductName: value.ProductName, ProductRate: value.ProductRate});
                    });
                    $scope.$apply(function () {
                        $scope.Products = newData;
                    });
                },
                error: function () {
                    //alert(_spPageContextInfo.webAbsoluteUrl);
                    alert("erro de conexão");
                }
            });
        };
        $scope.loadREST();
    }
</script>
    
asked by anonymous 20.02.2014 / 13:50

1 answer

3

Use dataType: "JSONP" in your ajax call! This will include the header required for cross domain requests. Important to note that you should use a version of jQuery greater than 1.7

    
20.02.2014 / 15:10